如何通过 10 个单选按钮使用 for 循环来查看选中了哪个

How do I use a for- loop through 10 radio buttons to see which one is checked

基本上我有 10 个单选按钮,当我单击其中一个时,我希望弹出一个消息框,其中包含与该单选按钮关联的数字。但是,我不想单独浏览每个单选按钮并添加一个 if radioButton1.checked then messageBox.show("1") 例如。 我创建了一个名为 CheckRadioButton 的事件处理程序,它将处理所有 10 个按钮事件,但我的 for 循环似乎不起作用...这是代码

Public Class Form1

Dim ListOFNames() As RadioButton = {RadioButton1, RadioButton2, RadioButton3, RadioButton4, RadioButton5, RadioButton6, RadioButton7, RadioButton8, RadioButton9,
RadioButton10}


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click

End Sub
Private Sub CheckRadioButton(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged, RadioButton3.CheckedChanged,
RadioButton4.CheckedChanged, RadioButton5.CheckedChanged, RadioButton6.CheckedChanged, RadioButton7.CheckedChanged, RadioButton8.CheckedChanged, RadioButton9.CheckedChanged, RadioButton10.CheckedChanged
    For counter = 0 To 9
        If ListOFNames(counter).Checked Then
            MessageBox.Show(counter)

        End If
    Next
End Sub
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged

End Sub

Private Sub TabPage1_Click(sender As Object, e As EventArgs) Handles TabPage1.Click

End Sub
End Class
Option Strict On
Public Class Form39
Private Sub RadioButton1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton1.CheckedChanged, RadioButton5.CheckedChanged, RadioButton4.CheckedChanged, RadioButton3.CheckedChanged, RadioButton2.CheckedChanged 'add more RadioButton  CheckedChanged event here
    Dim radio As Object = sender
    Dim radiobox = CType(radio, RadioButton)
    '// radiobox was checked
    '// do whatever you want to do next

End Sub
End Class

您可以在表单中的单选框之间使用 foreach 循环来检查单选框的选中情况。