首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在用户控制中,groupbox不充当带有无线电按钮的群盒。

在用户控制中,groupbox不充当带有无线电按钮的群盒。
EN

Stack Overflow用户
提问于 2014-05-08 20:26:00
回答 1查看 1.7K关注 0票数 1

我是VB.net的新手,试图创建一个用户控件,该控件包含一个包含可变数量的无线按钮的组盒,该控件分布均匀。分组盒和无线电按钮是不同的控制。我设法在表格上得到了带有无线电按钮的组盒,但我不明白为什么无线电按钮不作为一个组。(它们都可以一起检查)。

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

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

代码语言:javascript
运行
复制
Dim envGrpPanel As MyRadioGroupBox = New MyRadioGroupBox("Environments", arrNames, "")
With envGrpPanel
 .Dock = DockStyle.Fill
End With
tblContainerPanel.Controls.Add(envGrpPanel, 0, 0)

GROUPBOX超音速

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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

为了清晰起见,用户控制InitializeComponent GroupBox的一部分

代码语言:javascript
运行
复制
'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)

通过自定义无线电控制改变子循环

代码语言:javascript
运行
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-09 15:11:24

在Windows中没有自动的方法来执行您想做的事情,因为在设计期间似乎已经将包含在一个面板或GroupBox中的单选按钮的行为与单选按钮绑定在一起了。

但你可以很容易的解决这个问题。将单选按钮AutoCheck属性设置为False,这样它就不会尝试为您做任何特别的事情。然后侦听容器类中的单击事件,每当单击其中一个按钮时,检查所有按钮,检查单击的按钮,取消检查其余按钮。

它对我来说是这样的--为了简单我甚至摆脱了MyRadioButton,因为它不再是必需的了:

MyRadioGroupBox.vb

代码语言:javascript
运行
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23551838

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档