groupbox不会在用户控件中充当带有单选按钮的组框

时间:2014-05-08 20:26:00

标签: vb.net user-controls radio-button groupbox flowlayout

我是VB.net的新手并尝试创建一个用户控件,该控件包含一个包含可变数量的radiobutton均匀分布的groupbox。 groupbox和radiobutton是不同的控件。 我设法在表单上使用radiobutton获取groupbox,但我不明白为什么radiobutton不作为一个组。 (他们都可以一起检查。)

这是我到目前为止所拥有的;

调用控件并将其添加到表单

Dim envGrpPanel As MyRadioGroupBox = New MyRadioGroupBox("Environments", arrNames, "")
With envGrpPanel
 .Dock = DockStyle.Fill
End With
tblContainerPanel.Controls.Add(envGrpPanel, 0, 0)

GROUPBOX USERCONTROL

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

PS。当我手动将一些按钮添加到组框内的flowcontrol时,它可以正常工作。任何人吗?

USER CONTROL 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

for clearity,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)

通过自定义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

1 个答案:

答案 0 :(得分:0)

Windows窗体中没有自动方式可以执行您想要的操作,因为使一个Panel或GroupBox中包含的单选按钮的行为类似于单选按钮的逻辑似乎与在设计时已添加它们相关联。

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

它对我有用 - 我为了简单起见甚至摆脱了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