Link to home
Start Free TrialLog in
Avatar of BlakeMcKenna
BlakeMcKennaFlag for United States of America

asked on

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?
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

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

Avatar of ShareD_Point
ShareD_Point

Try the below code

rdBtn1.CheckedChanged += rdBtn1_CheckedChanged



Hope this helps.
Avatar of BlakeMcKenna

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks James..
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?
I figured it out sorta. I just changed the Event type from CheckChanged to Click and that worked.
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

I'll give that a try James...Thanks!