1

I am creating dynamic objects on a windows form, so far i have managed to create objects such as labels and radio buttons dynamically. However, now i am struggling with the event handling process. I know that i have to use AddressHandler and AddressOf ( as you can see from the code below)

Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click

    Dim i As Integer
    Dim radi As RadioButton
    For i = 1 To 4
        radi = New RadioButton
        radi.Location = New System.Drawing.Point(j, n)
        n = n + 60
        radi.Text = List(i)
        radi.Name = "rad" & i

        Me.Controls.Add(radi)
        AddHandler radi.CheckedChanged, AddressOf Me.RadioButton_Checked
    Next

End Sub
Private Sub RadioButton_Checked(ByVal sender As System.Object, ByVal e As System.EventArgs)
    If TypeOf sender Is RadioButton Then

    End If
    End If
End Sub

I need the code to output a message box in the case the user selects a specific option from the radio boxes. For example if they select "true" a msgbox should pop up.

Can someone give me some guidance on merely getting the code to recognise that the user has selected a radio button and to recognise the text of the radiobutton e.g. "true" , "wrong" etc.

Thanks in advance.

If you need any more clarification just ask.

2
  • 1
    TypeOf sender is always going to be RadioButton for a radiobutton click event - you need to cast sender to get at its properties . Look at the Text or store something in the Tag property Commented Nov 3, 2015 at 18:57
  • Plutonix Even though it's best practice to send the same object as the sender the first part is not always right. it can be different in some cases. Not in this case. of course because he's using the radiobutton class directly. Commented Nov 3, 2015 at 19:04

2 Answers 2

1

Try casting the sender:

With DirectCast(sender, RadioButton)
  If .Checked Then
    'Do Something
  End If
End With
Sign up to request clarification or add additional context in comments.

3 Comments

Will this allow the detection of the name of the radio button before 'doing something'
@M.Hasan You have access to all of the control's properties and methods inside that With Block.
Sorry for the nooby questions, I'm sure you can guess that I am a noob. Thanks.
1

You can use the Tag property and set it to some value that can help you identify the control with later.

radi.Tag = 1

and then

Dim radi as RadioButton = CType(sender, RadioButton)
if radi.Tag = 1 Then

End If

1 Comment

Your advice proved to be helpful as well. Thanks.

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.