【问题标题】:How to acces checked status of dynamicaly created Radio Buttons in VB如何在 VB 中访问动态创建的单选按钮的检查状态
【发布时间】:2018-02-07 10:38:02
【问题描述】:

我通过使用以下方法从数组中读取行,在标签页中创建了数量可变的单选按钮:

Public Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim rbgen As RadioButton
    Dim tab1 = 0
    For y As Integer = 0 To Array.GetUpperBound(0) Step 1
        If Array(y, 0) = "ABC" Then
            rbgen = New RadioButton
            rbgen.Name = "RButton" & Convert.ToString(y)
            rbgen.Left = 10
            rbgen.Top = ((tab1) * 30)
            rbgen.Text = Array(y, 2)
            rbgen.Size = New System.Drawing.Size(260, 40)
            TabPage1.Controls.Add(rbgen)
            tab1 = tab1 + 1
        End If
    Next 
End Sub

当我单击“开始”按钮时,我需要根据选中的 RadioButton 运行不同的代码。但是我如何访问检查了哪个单选按钮的信息?

非常感谢您的帮助!

【问题讨论】:

  • 你能循环通过你的控件检查一个选中的单选按钮。在该点退出循环并比较文本,可能在 Select Case 中确定接下来要运行的代码。
  • 感谢您的快速答复!我用这种方式试过,但没有用: For Each TypeOfControl As Control In Controls |对于每个 ChildType 作为 TypeOfControl.Controls 中的控件 |如果 TypeOf ChildType 是 RadioButton 则 | Dim StateOfRadioButton As RadioButton = DirectCast(ChildType, RadioButton)Then.... 你知道为什么吗?

标签: vb.net visual-studio radio-button tabpage


【解决方案1】:

您可以在添加单选按钮后尝试添加处理程序:

AddHandler rbgen.CheckedChanged, AddressOf RadioBox_CheckedChanged

然后你需要添加这个子

Private Sub RadioBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

Dim RadioBox As RadioButton = TryCast(sender, RadioButton)

If RadioBox IsNot Nothing Then

    MessageBox.Show(RadioBox.CheckState)

End If

End Sub

如果你想检查状态

For Each RadioBox In TabPage1.Controls.OfType(Of RadioButton)()
if Ctype(TabPage1.controls("RadioButton" & i), radiobutton).checked = true then
 'your Code Here
end if
next

我还不能测试它,但我希望我已经帮助了你一点:)

【讨论】:

  • 非常感谢!你帮了我很多,你的代码运行良好!只是多了一个问题。如果我更改选项卡,则在另一个选项卡中选择的最后一个单选按钮仍处于活动状态/选中状态。我该如何改变这个?
猜你喜欢
  • 1970-01-01
  • 2016-01-09
  • 2013-02-21
  • 2013-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-09
  • 2011-07-29
相关资源
最近更新 更多