【问题标题】:groupbox does not act as groupbox with radiobuttons in usercontrolgroupbox 不充当用户控件中带有单选按钮的 groupbox
【发布时间】: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


【解决方案1】:

Windows 窗体中没有自动的方式来执行您想要的操作,因为使包含在一个 Panel 或 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

【讨论】:

  • 这对我来说很有意义。为方便起见,我将子按钮添加到 mainForm 中。尝试此操作时出现错误(可能是新手错误): For Each oControl In FlowLayoutPanel1.Controls -> Reference to A Non shared member。当我添加 Friend WithEvents FlowLayoutPanel1 As System.Windows.Forms.FlowLayoutPanel 时,我收到两个错误:MyRnd 未声明且 *Operator 未定义类型跨度>
  • For Each oControl In FlowLayoutPanel1.Controls -&gt; Reference to A Non shared member - 我认为这只是缺少Me,例如... In Me.FlowLayoutPanel1.Controls。 *** myRnd vs. myRdn 只是一个错字,我会在答案中编辑它。 *** 我不知道Operator&lt;&gt; 错误,也许它会随着其他更改而消失。
  • 没有任何区别;有或没有我。不明白,虽然codecompletion显示FlowLayoutPanel1.
  • 稍微改变了循环,现在它通过循环运行,但是(其他)自定义单选按钮没有取消选中?您可以全部查看。
  • 好的,我终于尝试在这里重新创建您的代码并让它为我工作。我几乎重写了整个答案,请再读一遍。
猜你喜欢
  • 2012-06-25
  • 1970-01-01
  • 1970-01-01
  • 2019-07-31
  • 2019-06-16
  • 1970-01-01
  • 2013-10-19
  • 2019-01-03
  • 2012-03-31
相关资源
最近更新 更多