【发布时间】:2014-06-26 10:33:02
【问题描述】:
我是 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
ps。当我手动将一些按钮添加到 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
为清楚起见,用户控件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
【问题讨论】:
-
即使将一组名称(?)传递给新的 GroupBox,我也看不到您在哪里创建了多个 RB。第一个代码块表示您在运行时创建/添加,但 PS 中的“手册”是否意味着 IDE/设计时?您可以调试 CheckChanged 事件以检查
Parent属性的值以查看它们被添加到的内容。如果可以一次检查所有这些,则它们不能具有与父级相同的容器控件。 -
什么是
MyRadioButton?它是继承RadioButton的控件还是其他东西?您的代码没有意义,因为它使用了未声明的变量i。要么您正在使用一个未向我们展示的循环,要么您可能打算使用一个循环但没有。 -
感谢您的评论并指出我缺少的部分。我更正了代码。我在运行时创建/添加控件。基本用户控件在 VS 中作为用户控件 (IDE/GUI) 创建。实际上,我的意思是当我手动将一些单选按钮添加到流控制(在组框内)时它可以工作= IDE / GUI 调试父级的更改事件时(在子 RadioButton1_CheckedChanged 中停止调试器时在即时窗口中键入):?Parent .Name "FlowLayoutPanel1"
标签: vb.net user-controls radio-button groupbox flowlayout