1

尝试将事件处理程序添加到在用户输入上创建的单选按钮,以thisthis作为示例。

但是,我收到一个answerOptions不是事件的错误。

创建单选按钮

Private answerOptions(n) As RadioButton
...    
Private Sub showQuestion(n As Integer)

For i = 0 To answerOptions.Length - 1
    answerOptions(i) = New RadioButton
    AddHandler answerOptions, AddressOf Me.Radios_Click
    With answerOptions(i)
        ' --------- SET TEXT, LOCATION ETC.
    End With
    Me.Controls.Add(answerOptions(i))
Next
End Sub

计划然后处理事件

Private Sub Radios_Click(sender As Object, e As EventArgs) Handles answerOptions.checked

End Sub

我希望在检查无线电时发生一些事情。不想使用复选框,因为我想一次限制一个选择。

4

1 回答 1

1

在您的表单或页面中尝试此操作

    Private Sub ClickButton(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim Rbtn As RadioButton
    Rbtn = CType(sender, RadioButton)
    MsgBox(Rbtn.Text)
End Sub

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

    Dim i As Integer
    Dim Rbtn As New RadioButton()

    For i = 0 To 19
        Rbtn = New RadioButton()
        With Rbtn
            .Name = "RBtn" & i
            .Text = .Name
            .Checked = False
            .Left = 20
            .Top = (i * 20)
            .Visible = True
            Me.Controls.Add(Rbtn)
            AddHandler Rbtn.Click, AddressOf ClickButton
        End With
    Next

End Sub
于 2012-12-17T02:25:45.017 回答