Creating Events for dynamic controls?

My windows app has a form in which a hand full of RadioButton controls are created dynamically. Behind each of these RDO's there needs to be code. My problem is that I don't know how to create the "CheckChanged()" Event and tie them into the dynamically created controls. Is this possible?
BlakeMcKennaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)PresidentCommented:
Create the event procedure as you do usually with a RadioButton (create a temporary button if needed), and remove the Handles at the end of the Sub declaration.

Link it with your dynamic controls with AddHandler, with a syntax like the following:

AddHandler yourRadioButton.CheckedChanged, AddressOf DynamicRB_CheckedChanged

Private Sub DynamicRB_CheckedChanged(sender As Object, e As EventArgs)
    'Your code for the event
End Sub

Open in new window

0
ShareD_PointCommented:
Try the below code

rdBtn1.CheckedChanged += rdBtn1_CheckedChanged



Hope this helps.
0
BlakeMcKennaAuthor Commented:
James,

Here is the code that creates the dynamic RDO's. Also, please see the attached image.
Will this single Event cover all the dynamic RDO's?


        Private Sub LoadAvailableExquipmentTypes()
        Try
            Dim col As New Collection
            Dim rdo As RadioButton
            Dim xCol As Integer = 16
            Dim pointX As Integer = 0
            Dim x As Integer = 0

            strErr = ""
            col = BL.GetEquipmentType

            For Each itm In col
                x += 1
                rdo = New RadioButton
                rdo.Tag = x
                rdo.Text = itm
                rdo.AutoSize = True
                rdo.Name = "rdoST" & x
                AddHandler rdo.CheckedChanged, AddressOf rdoST_CheckedChanged
                rdo.Font = New Font(rdo.Font.Name, rdo.Font.Size, Drawing.FontStyle.Regular)
                pnlEquipmentType.Controls.Add(rdo)
                rdo.Location = New Point(xCol, 3)
                pointX = rdo.Width + 30
                xCol += pointX
            Next

        Catch ex As Exception
            strErr = gfrmID & "/LoadAvailableExquipmentTypes() - " & ex.Message
        End Try

        ProcessMessages(Me, sbr, strErr)
    End Sub
0
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Jacques Bourgeois (James Burger)PresidentCommented:
Yes, rdoST_CheckedChanged will handle the event for all your RadioButtons.

If you need to reference the RadioButton that triggered the event, simply use the sender parameter of the event procedure. Since it is defined as an Object, you will have to cast it to a RadioButton, something like the following:

Private Sub rdoST_CheckedChanged(sender As Object, e As EventArgs)
     DirectCast(sender, RadioButton).Text = "You clicked me"
End Sub

@ShareD_Point

This is a VB question, not C#. One of the biggest differences between the 2 languages is the syntax they use to work with delegates/events.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ShareD_PointCommented:
Thanks James..
0
BlakeMcKennaAuthor Commented:
Hi James,

I tried your code and it works. However, here is my code below.

        Private Sub rdoST_CheckedChanged(sender As Object, e As EventArgs)
        Try
            Dim rdo As RadioButton = DirectCast(sender, RadioButton)

            strErr = ""

            Select Case rdo.Tag
                Case "1"
                    MsgBox(rdo.Tag)
                Case "2"
                    MsgBox(rdo.Tag)
                Case "3"
                    MsgBox(rdo.Tag)
                Case "4"
                    MsgBox(rdo.Tag)
                Case "5"
                    MsgBox(rdo.Tag)
            End Select

        Catch ex As Exception
            strErr = gfrmID & "/rdoST_CheckedChanged() - " & ex.Message
        End Try

        ProcessMessages(Me, sbr, strErr)
    End Sub

What's happening in this scenario is that when I click on a RDO the first time, it displays the correct value in the Tag property, however, when I click on a different one, it will display the value of the previous Tag value first then the current Tag value. So essentially, it's executing this Sub back-to-back. I know this because I ran it thru the debugger. Not exactly sure why it's doing this?
0
BlakeMcKennaAuthor Commented:
I figured it out sorta. I just changed the Event type from CheckChanged to Click and that worked.
0
Jacques Bourgeois (James Burger)PresidentCommented:
What was happening is that when you check a RadioButton, the RadioButton that was previously checked should first be unchecked, which also triggers a CheckedChange.

If I were you, I would bring back the code in CheckedChange, because in the Click, it would trigger if the user clicks a second time on a RadioButton that is already checked, which might not be something that you want to happen.

Simply add a little "check" in the code so that it triggers only when Checked property is activated, not when it is deactivated:
        Private Sub rdoST_CheckedChanged(sender As Object, e As EventArgs)
        Try
            Dim rdo As RadioButton = DirectCast(sender, RadioButton)

If rdo.Checked Then

            strErr = ""

            Select Case rdo.Tag
                Case "1"
                    MsgBox(rdo.Tag)
                Case "2"
                    MsgBox(rdo.Tag)
                Case "3"
                    MsgBox(rdo.Tag)
                Case "4"
                    MsgBox(rdo.Tag)
                Case "5"
                    MsgBox(rdo.Tag)
            End Select

        Catch ex As Exception
            strErr = gfrmID & "/rdoST_CheckedChanged() - " & ex.Message
        End Try

        ProcessMessages(Me, sbr, strErr)

End If

    End Sub

Open in new window

0
BlakeMcKennaAuthor Commented:
I'll give that a try James...Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.