Vb.net groupbox在usercontrol中不充当带有单选按钮的groupbox

Vb.net groupbox在usercontrol中不充当带有单选按钮的groupbox,vb.net,user-controls,radio-button,groupbox,flowlayout,Vb.net,User Controls,Radio Button,Groupbox,Flowlayout,我是VB.net的新手,正在尝试创建一个用户控件,该控件包含一个分组框,该分组框包含均匀分布的可变数量的单选按钮。groupbox和radiobutton是不同的控件。 我设法在表单上获得带有单选按钮的groupbox,但我不理解为什么单选按钮不作为一个组。(它们都可以一起检查) 这就是我目前所拥有的 调用控件并将其添加到窗体 GROUPBOX用户控件 Imports System.Windows.Forms Imports JIM.MyRadioButton Public Class MyR

我是VB.net的新手,正在尝试创建一个用户控件,该控件包含一个分组框,该分组框包含均匀分布的可变数量的单选按钮。groupbox和radiobutton是不同的控件。 我设法在表单上获得带有单选按钮的groupbox,但我不理解为什么单选按钮不作为一个组。(它们都可以一起检查)

这就是我目前所拥有的

调用控件并将其添加到窗体

GROUPBOX用户控件

Imports System.Windows.Forms
Imports JIM.MyRadioButton

Public Class MyRadioGroupBox
Inherits UserControl

Public Sub New(ByVal grpBoxName As String, ByVal controlValues As Array, _ 
ByVal construct As Object)

 InitializeComponent()
 Me.GroupBox.Text = grpBoxName

 For i As Integer = 0 To controlValues.Length - 1
  Dim myRdn As MyRadioButton = New MyRadioButton(controlValues.GetValue(i), i)
  myRdn.AutoSize = True
  myRdn.Dock = DockStyle.Fill
  Me.FlowLayoutPanel1.Controls.Add(myRdn)
 End Sub
End Class
另外,当我在groupbox中手动添加一些按钮到flowcontrol时,它工作正常。有人吗

用户控件MyRadioButton

Imports System.Windows.Forms
Imports JIM.MyRadioButton

Public Class MyRadioButton
 Inherits UserControl

 Public Event rbnClick(ByVal sender As MyRadioButton, ByVal radioButtonName As System.EventArgs)

  Public Sub New(ByVal btnText As String, ByVal tabStop As Integer)
  InitializeComponent()

  Me.RadioButton.Text = btnText
  AddHandler Me.RadioButton.CheckedChanged, AddressOf RadioButton_CheckedChanged
 End Sub

  Private Sub RadioButton_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton.CheckedChanged
        RaiseEvent rdnBtnClicked(sender, e)
  End Sub
End Class
为了清晰起见,usercontrol GroupBox的InitializeComponent的一部分

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.  
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
    Me.GroupBox = New System.Windows.Forms.GroupBox()
    Me.FlowLayoutPanel1 = New System.Windows.Forms.FlowLayoutPanel()
    Me.GroupBox.SuspendLayout()
    Me.SuspendLayout()
    '
    'GroupBox
    '
    Me.GroupBox.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None
    Me.GroupBox.Controls.Add(Me.FlowLayoutPanel1)
'注意:Windows窗体设计器需要以下过程
'可以使用Windows窗体设计器对其进行修改。
'不要使用代码编辑器修改它。
_
私有子初始化组件()
Me.GroupBox=New System.Windows.Forms.GroupBox()
Me.FlowLayoutPanel 1=新建System.Windows.Forms.FlowLayoutPanel()
Me.GroupBox.SuspendLayout()
Me.SuspendLayout()
'
“GroupBox
'
Me.GroupBox.BackgroundImageLayout=System.Windows.Forms.ImageLayout.None
Me.GroupBox.Controls.Add(Me.FlowLayoutPanel1)
通过自定义RadioControl更改了子循环

Private Sub rdnBtnClicked(ByVal sender As MyRadioButton, ByVal e As System.EventArgs)
        For Each oControl As Object In FlowLayoutPanel1.Controls
            Dim myRdn As MyRadioButton = oControl
            System.Console.WriteLine("MyRdn: " & myRdn.Name & ". Sender.name: " & sender.Name)
            If myRdn.Name <> sender.Name Then
                ' Not the one which has just been set, uncheck it
                myRdn.Checked = False
            End If
        Next

    End Sub
Private Sub rdnbtclicked(ByVal发送方作为MyRadioButton,ByVal e作为System.EventArgs)
对于FlowLayoutPanel1.Controls中作为对象的每个oControl
将myRdn设置为MyRadioButton=oControl
System.Console.WriteLine(“MyRdn:&MyRdn.Name&.”Sender.Name:&Sender.Name)
如果myRdn.Name sender.Name,则
'不是刚刚设置的,请取消选中它
myRdn.Checked=False
如果结束
下一个
端接头

Windows窗体中没有自动方式来完成您想要的操作,因为在设计期间添加单选按钮时,将单选按钮封装在一个面板或GroupBox中的逻辑似乎与单选按钮绑定在一起

但是你可以很容易地解决这个问题。将单选按钮
AutoCheck
属性设置为
False
,以便它不会尝试为您执行任何特殊操作。然后在容器类中侦听clicked事件,每当单击其中一个按钮时,检查所有按钮,检查单击的按钮,取消选中其余按钮

它对我来说是这样的——为了简单起见,我甚至去掉了
MyRadioButton
,因为它不再需要了:

MyRadioGroupBox.vb

Public Class MyRadioGroupBox
    Inherits UserControl

    Public Sub New(ByVal grpBoxName As String, ByVal controlValues As Array, _
    ByVal construct As Object)

        InitializeComponent()
        Me.GroupBox.Text = grpBoxName

        For i As Integer = 0 To controlValues.Length - 1
            ' Create a regular RadioButton
            Dim myRdn As RadioButton = New RadioButton
            myRdn.Text = controlValues.GetValue(i)
            myRdn.AutoSize = True
            ' Disable its AutoCheck functionality
            myRdn.AutoCheck = False
            myRdn.Dock = DockStyle.Fill
            Me.FlowLayoutPanel1.Controls.Add(myRdn)
            ' Register for its Click event
            AddHandler myRdn.Click, AddressOf MyRadioGroupBox_rdnBtnClicked
        Next
    End Sub

    Private Sub MyRadioGroupBox_rdnBtnClicked(sender As Object, e As EventArgs)
        ' For all child controls in the panel...
        For Each oControl As Object In FlowLayoutPanel1.Controls
            ' ...get it as a RadioButton and compare with the event sender,
            ' i.e. the button which has just been clicked.
            Dim myRdn As RadioButton = oControl
            If Object.ReferenceEquals(myRdn, sender) Then
                ' Match - it's the one which has just been clicked, check it
                myRdn.Checked = True
            Else
                ' Does not match - it's some other one, uncheck it
                myRdn.Checked = False
            End If
        Next
    End Sub

End Class

即使将名称数组(?)传递给新的GroupBox,我也看不到您在何处创建了多个RB。第一个代码块表示您正在运行时创建/添加,但PS中的“手动”是否表示在IDE/设计时?您可以调试CheckChanged事件以检查
Parent
属性的值,查看它们添加到了什么。如果可以同时检查所有容器,则它们不能与父容器具有相同的容器控件。什么是
MyRadioButton
?它是继承单选按钮的控件还是其他控件?您的代码没有意义,因为它使用了未声明的变量
i
。您正在使用一个尚未向我们展示的循环,或者您可能打算使用一个循环,但没有。感谢您的评论,并为我指出缺少的部分。我更正了代码。我在运行时创建/添加控件。基本用户控件在VS中创建为用户控件(IDE/GUI)。事实上,我的意思是,在调试父对象的changeevent(当调试器在子RadioButton1_CheckedChanged中停止时,在即时窗口中键入):?parent.Name“FlowLayoutPanel1”这对我来说很有意义。为了方便起见,我移动了sub,将单选按钮添加到mainForm。尝试此操作时,会出现错误(可能是新手错误):对于FlowLayoutPanel1.Controls->对非共享成员的引用中的每个oControl。当我将Friend WithEvents FlowLayoutPanel1添加为System.Windows.Forms.FlowLayoutPanel时,我收到两个错误:没有声明MyRnd,并且没有为FlowLayoutPanel1.Controls中的每个oControl的类型
定义*运算符。Controls->对非共享成员的引用
-我认为这只是缺少了
Me
,比如
。。。在Me.FlowLayoutPanel1.控件中
**
myRnd
vs.
myRdn
只是一个输入错误,我将在答案中编辑它。***我不知道
操作符
的错误,也许它会随着其他更改而消失。它没有任何区别;不管有没有我。不明白,当codecompletion显示FlowLayoutPanel1时,对循环进行了一些更改,现在它在循环中运行,但是(其他)自定义单选按钮没有取消选中?你可以全部检查。好的,我终于尝试在这里重新创建你的代码,并让它为我工作。我几乎重写了全部答案,请再读一遍。
Public Class MyRadioGroupBox
    Inherits UserControl

    Public Sub New(ByVal grpBoxName As String, ByVal controlValues As Array, _
    ByVal construct As Object)

        InitializeComponent()
        Me.GroupBox.Text = grpBoxName

        For i As Integer = 0 To controlValues.Length - 1
            ' Create a regular RadioButton
            Dim myRdn As RadioButton = New RadioButton
            myRdn.Text = controlValues.GetValue(i)
            myRdn.AutoSize = True
            ' Disable its AutoCheck functionality
            myRdn.AutoCheck = False
            myRdn.Dock = DockStyle.Fill
            Me.FlowLayoutPanel1.Controls.Add(myRdn)
            ' Register for its Click event
            AddHandler myRdn.Click, AddressOf MyRadioGroupBox_rdnBtnClicked
        Next
    End Sub

    Private Sub MyRadioGroupBox_rdnBtnClicked(sender As Object, e As EventArgs)
        ' For all child controls in the panel...
        For Each oControl As Object In FlowLayoutPanel1.Controls
            ' ...get it as a RadioButton and compare with the event sender,
            ' i.e. the button which has just been clicked.
            Dim myRdn As RadioButton = oControl
            If Object.ReferenceEquals(myRdn, sender) Then
                ' Match - it's the one which has just been clicked, check it
                myRdn.Checked = True
            Else
                ' Does not match - it's some other one, uncheck it
                myRdn.Checked = False
            End If
        Next
    End Sub

End Class