Accessing Events for controls that were added programmically

Electron

Newcomer
Joined
Mar 8, 2003
Messages
7
I am adding an array of buttons programmically and I have no idea on how to access the click procedure. Can anyone help me, Please?

This is for adding the buttons:

Code:
        Dim B(5) As Button
        Dim int3 As Integer = 1
        For int3 = 1 To 5
            B(int3) = New Button()
            Me.Controls.Add(B(int3))
            B(int3).Size = New Size(50, 20)
            B(int3).FlatStyle = FlatStyle.Standard
            B(int3).Location = New Point(305, int3 * 25)
            B(int3).Text = "Stats"
            B(int3).Visible = True
            B(int3).Name = "b" & int3
            AddHandler B(int3).Click, AddressOf ClickHandler
        Next

This is a sub with the events in it

Code:
    Public Sub ClickHandler(ByVal sender As Object, ByVal e As System.EventArgs)
        If sender.GetType.Name = "b1" Then
            MsgBox("yes")
        End If
    End Sub

Thanks for your help, in advance.

Electron
 
Is the problem that you need to establish in the click procedure which button raised the event?
 
As each button has a unique name, and you can easily CType(Sender, Control) you should have no problem to determine the name and the sending control itself.

Also, by parsing the name

something like ...
Visual Basic:
Dim indexOfSender as integer = 1
Dim ctrl as control = CType(Sender, Control)
indexOfSender = CInt(Control.Name.Substring(x,y)) 'Dunno exactly

B(indexOfSender).performWildOperations

Of course the requires B to be a member variable of the class, not a local variable in a sub.
 
Try this: note the change in name

Visual Basic:
Dim B(5) As Button
        Dim int3 As Integer = 1
        For int3 = 1 To 5
            B(int3) = New Button()
            Me.Controls.Add(B(int3))
            B(int3).Size = New Size(50, 20)
            B(int3).FlatStyle = FlatStyle.Standard
            B(int3).Location = New Point(305, int3 * 25)
            B(int3).Text = "Stats"
            B(int3).Visible = True
            B(int3).Name =  int3.tostring("0")
            AddHandler B(int3).Click, AddressOf ClickHandler
        Next

The sub will be

Visual Basic:
    Private Sub ClickHandler(ByVal sender As Object, ByVal e As EventArgs)
        Dim ID as integer
        ID = CInt(DirectCast(DirectCast(sender, Button).Name, String))
' if ID = i, then button B(i) was clicked
end sub
 
Last edited by a moderator:
I dont know if im too late but I dont think anybody offered this solution (correct me if im wrong :( ) :

Make the Dim B(5) As Button public to the class by dimming it outside the sub and try this:

Visual Basic:
Dim int3 As Integer = 1
For int3 = 1 To 5
            B(int3) = New Button()
            Me.Controls.Add(B(int3))
            B(int3).Size = New Size(50, 20)
            B(int3).FlatStyle = FlatStyle.Standard
            B(int3).Location = New Point(305, int3 * 25)
            B(int3).Text = "Stats"
            B(int3).Visible = True
            B(int3).Name = "b" & int3
            AddHandler B(int3).Click, AddressOf ClickHandler
Next

Visual Basic:
Public Sub ClickHandler(ByVal sender As Object, ByVal e As System.EventArgs)
If sender Is b(somenumber) then
     MsgBox("yes")
End If
End Sub
 
Back
Top