我是VB.net的新手,并试图创建一个用户控件,该控件包含均匀分布的可变数量的单选按钮的组框。组框和单选按钮是不同的控件。我设法在表单上获得带有单选按钮的组框,但我不明白为什么单选按钮不作为一个组。(它们可以一起检查)。
这是我目前为止看到的;
调用控件并将其添加到窗体
Dim envGrpPanel As MyRadioGroupBox = New MyRadioGroupBox("Environments", arrNames, "")
With envGrpPanel
.Dock = DockStyle.Fill
End With
tblContainerPanel.Controls.Add(envGrpPanel, 0, 0)
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
p。当我在组框内手动添加一些按钮到流控制时,它可以正常工作。有人知道吗?
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
为清楚起见,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)
通过自定义无线电控件更改子循环
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
在Windows窗体中没有自动的方法来做你想做的事情,因为使一个面板或组框中包含的单选按钮表现得像单选按钮的逻辑似乎已经与在设计时添加的它们联系在一起了。
但是你可以很容易地绕过它。将单选按钮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