如何在VB中访问动态创建的单选按钮的已检查状态

时间:2018-02-07 10:38:02

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

我通过使用以下方法读取数组中的行来在Tab页面中创建可变数量的单选按钮:

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运行不同的代码。但是如何访问检查哪个单选按钮的信息?

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

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

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

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