首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >嵌套RadioButtons的GroupBoxes行为

嵌套RadioButtons的GroupBoxes行为
EN

Stack Overflow用户
提问于 2014-02-06 19:33:43
回答 1查看 772关注 0票数 0

我想知道是否可以使用GroupBox作为纯粹的审美对象。简单地说,我有一个很大的组盒,里面有很多小的组框。他们都有单选按钮。我希望用户只能激活其中的一个单选按钮。

现在,在构建了想要的用户界面之后,我有一个不想要的行为,即单选按钮由内部大多数组框控制,因此用户可以在每个小组框中激活一个。

我遵循一个指导方针,所以我不想修改用户界面的构建方式。我不确定嵌套组框是否有意义,但我需要遵循这条道路。是否有一个明确的属性来判断哪个物体控制一个给定的无线电按钮?

我正在使用VisualStudio2010Professional。

EN

回答 1

Stack Overflow用户

发布于 2014-02-06 21:39:14

好吧,既然我没有收到任何答案,我想我会.

另外,您的单选按钮的个别事件仍然是单独的,并且您也可以对它们进行更改,这样您就不会将每个单选按钮组合在一起。

更新:可以在组框中有嵌套组框,也可以单独使用组框,如果需要,可以切换此行为。。。

代码语言:javascript
运行
复制
 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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21612261

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档