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

How to acces checked status of dynamicaly created Radio Buttons in VB

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

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

当我单击 "start" 按钮时,我需要 运行 不同的代码,具体取决于选中的 RadioButton。但是我如何访问选中哪个单选按钮的信息?

非常感谢您的帮助!

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

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

我还不能测试它,但我希望我已经对你有所帮助:)