1

Trying to add event handlers to radio buttons that are created on user input, looking at this and this as examples.

However I get an error that answerOptions is not an event.

Create the radio buttons

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

Planning on then handling events with

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

End Sub

I want things to happen when the radios are checked. Don't want to use checkboxes as I want to limit one selection at a time.

1 Answer 1

1

Try this in your form or page

    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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.