BlakeMcKenna
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?
Try the below code
rdBtn1.CheckedChanged += rdBtn1_CheckedChanged
Hope this helps.
rdBtn1.CheckedChanged += rdBtn1_CheckedChanged
Hope this helps.
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 LoadAvailableExquipmentTyp es()
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 & "/LoadAvailableExquipmentT ypes() - " & ex.Message
End Try
ProcessMessages(Me, sbr, strErr)
End Sub
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 LoadAvailableExquipmentTyp
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.
rdo.Location = New Point(xCol, 3)
pointX = rdo.Width + 30
xCol += pointX
Next
Catch ex As Exception
strErr = gfrmID & "/LoadAvailableExquipmentT
End Try
ProcessMessages(Me, sbr, strErr)
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanks James..
ASKER
Hi James,
I tried your code and it works. However, here is my code below.
Private Sub rdoST_CheckedChanged(sende r 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 tried your code and it works. However, here is my code below.
Private Sub rdoST_CheckedChanged(sende
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?
ASKER
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:
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
ASKER
I'll give that a try James...Thanks!
Link it with your dynamic controls with AddHandler, with a syntax like the following:
Open in new window