我想知道是否可以将GroupBox用作纯粹的美学对象。简单地说,我有一个大的组合框,里面有许多小组框。他们都有单选按钮。我希望用户只能激活其中的一个单选按钮。
现在,在构建想要的用户界面之后,我有一个不受欢迎的行为,即单选按钮由最内层的组框决定,因此用户可以在每个小组框中激活一个。
我遵循指南,所以我不想修改用户界面的构造方式。我不确定嵌套组合框是否有意义,但我需要遵循这条路径。是否有一个显式属性来告诉哪个对象管理给定的单选按钮?
我正在使用Visual Studio 2010 Professional。
答案 0 :(得分:1)
好吧,好吧,因为我看到没有提交答案,我以为我会...
此外,您的单选按钮的个别事件仍然是个人的,您也可以对它们进行更改,这样您就不会影响每个单选按钮。
更新:您可以在组框中嵌套组框或单独使用组框,您可以根据需要切换此行为。 。 强>
Public Class TestRadioButtons
Private blnIsGroupBoxInGroupBox As Boolean = False
Private Sub Form2_Disposed(sender As Object, e As System.EventArgs) Handles Me.Disposed
RemoveHandlers()
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
blnIsGroupBoxInGroupBox = False
AddHandlers()
For Each ctrl As Control In Me.Controls
If TypeOf (ctrl) Is Windows.Forms.GroupBox Then
For Each rB As RadioButton In ctrl.Controls
rB.AutoCheck = True
Next
End If
Next
For Each ctrl As Control In Me.Controls
If TypeOf (ctrl) Is Windows.Forms.GroupBox Then
For Each rB As RadioButton In ctrl.Controls
rB.Checked = False
Next
End If
Next
End Sub
Private Sub PerformCheck(ByVal sender As Object, e As System.EventArgs)
Select Case blnIsGroupBoxInGroupBox
Case True
For Each cntl As Control In Me.Controls
If TypeOf (cntl) Is Windows.Forms.GroupBox Then
For Each con As Windows.Forms.GroupBox In cntl.Controls
For Each rB As RadioButton In con.Controls
If rB.Name.ToString = sender.name.ToString Then
If rB.Checked Then
For Each cont As Control In Me.Controls
If TypeOf (cont) Is Windows.Forms.GroupBox Then
For Each gBox As Windows.Forms.GroupBox In cont.Controls
For Each rButton As RadioButton In gBox.Controls
If Not rButton.Name.Equals(sender.Name.ToString) Then
rButton.Checked = False
End If
Next
Next
End If
Next
End If
End If
Next
Next
End If
Next
Case Else
For Each ctrl As Control In Me.Controls
If TypeOf (ctrl) Is Windows.Forms.GroupBox Then
For Each rB As RadioButton In ctrl.Controls
If rB.Name = sender.Name.ToString Then
If rB.Checked Then
For Each con As Control In Me.Controls
If TypeOf (con) Is Windows.Forms.GroupBox Then
For Each rButton As RadioButton In con.Controls
If Not rButton.Name.Equals(sender.Name.ToString) Then
rButton.Checked = False
End If
Next
End If
Next
End If
End If
Next
End If
Next
End Select
End Sub
Private Sub AddHandlers()
Select Case blnIsGroupBoxInGroupBox
Case True
For Each ctrl As Control In Me.Controls
If TypeOf (ctrl) Is Windows.Forms.GroupBox Then
For Each con As Windows.Forms.GroupBox In ctrl.Controls
For Each rB As RadioButton In con.Controls
AddHandler rB.CheckedChanged, AddressOf PerformCheck
Next
Next
End If
Next
Case Else
For Each ctrl As Control In Me.Controls
If TypeOf (ctrl) Is Windows.Forms.GroupBox Then
For Each rB As RadioButton In ctrl.Controls
AddHandler rB.CheckedChanged, AddressOf PerformCheck
Next
End If
Next
End Select
End Sub
Private Sub RemoveHandlers()
If Me IsNot Nothing Then
Select Case blnIsGroupBoxInGroupBox
Case True
For Each ctrl As Control In Me.Controls
If TypeOf (ctrl) Is Windows.Forms.GroupBox Then
For Each con As Windows.Forms.GroupBox In ctrl.Controls
For Each rB As RadioButton In con.Controls
RemoveHandler rB.CheckedChanged, AddressOf PerformCheck
Next
Next
End If
Next
Case Else
For Each ctrl As Control In Me.Controls
If TypeOf (ctrl) Is Windows.Forms.GroupBox Then
For Each rB As RadioButton In ctrl.Controls
RemoveHandler rB.CheckedChanged, AddressOf PerformCheck
Next
End If
Next
End Select
End If
End Sub
End Class