Imports System Imports System.Collections.Generic Imports System.Linq Imports System.Text Imports System.Windows.Forms Imports System.Diagnostics Imports System.Windows.Forms.VisualStyles Imports System.Drawing Imports System.ComponentModel Namespace Use.your.own Public Class AdvancedRadioButton Inherits CheckBox Public Enum Level Parent Form End Enum <Category("AdvancedRadioButton"), Description("Gets or sets the level that specifies which RadioButton controls are affected."), DefaultValue(Level.Parent)> Public Property GroupNameLevel As Level <Category("AdvancedRadioButton"), Description("Gets or sets the name that specifies which RadioButton controls are mutually exclusive.")> Public Property GroupName As String Protected Overrides Sub OnCheckedChanged(ByVal e As EventArgs) MyBase.OnCheckedChanged(e) If Checked Then Dim arbControls = CType(Nothing, dynamic) Select Case GroupNameLevel Case Level.Parent If Me.Parent IsNot Nothing Then arbControls = GetAll(Me.Parent, GetType(AdvancedRadioButton)) Case Level.Form Dim form As Form = Me.FindForm() If form IsNot Nothing Then arbControls = GetAll(Me.FindForm(), GetType(AdvancedRadioButton)) End Select If arbControls IsNot Nothing Then For Each control As Control In arbControls If control <> Me AndAlso (TryCast(control, AdvancedRadioButton)).GroupName = Me.GroupName Then (TryCast(control, AdvancedRadioButton)).Checked = False Next End If End If End Sub Protected Overrides Sub OnClick(ByVal e As EventArgs) If Not Checked Then MyBase.OnClick(e) End Sub Protected Overrides Sub OnPaint(ByVal pevent As PaintEventArgs) CheckBoxRenderer.DrawParentBackground(pevent.Graphics, pevent.ClipRectangle, Me) Dim radioButtonState As RadioButtonState If Checked Then radioButtonState = RadioButtonState.CheckedNormal If Focused Then radioButtonState = RadioButtonState.CheckedHot If Not Enabled Then radioButtonState = RadioButtonState.CheckedDisabled Else radioButtonState = RadioButtonState.UncheckedNormal If Focused Then radioButtonState = RadioButtonState.UncheckedHot If Not Enabled Then radioButtonState = RadioButtonState.UncheckedDisabled End If Dim glyphSize As Size = RadioButtonRenderer.GetGlyphSize(pevent.Graphics, radioButtonState) Dim rect As Rectangle = pevent.ClipRectangle rect.Width -= glyphSize.Width rect.Location = New Point(rect.Left + glyphSize.Width, rect.Top) RadioButtonRenderer.DrawRadioButton(pevent.Graphics, New System.Drawing.Point(0, rect.Height / 2 - glyphSize.Height / 2), rect, Me.Text, Me.Font, Me.Focused, radioButtonState) End Sub Private Function GetAll(ByVal control As Control, ByVal type As Type) As IEnumerable(Of Control) Dim controls = control.Controls.Cast(Of Control)() Return controls.SelectMany(Function(ctrl) GetAll(ctrl, type)).Concat(controls).Where(Function(c) c.[GetType]() = type) End Function End Class End Namespace
If Checked Then Dim arbControls = CType(Nothing, Dynamic)
If control <> Me AndAlso (TryCast(control, AdvancedRadioButton)).GroupName = Me.GroupName Then (TryCast(control, AdvancedRadioButton)).Checked = False
If control IsNot Me AndAlso Equals(TryCast(control, AdvancedRadioButton).GroupName, GroupName) Then TryCast(control, AdvancedRadioButton).Checked = False
Dim arbControls = CType(Nothing, dynamic)
Dim arbControls = Nothing, Dynamic