Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

 

       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

 

   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

Posted

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 ...

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.

.nerd
Posted (edited)

Try this: note the change in name

 

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

 

   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

Edited by divil
Guest mutant
Posted

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:

 

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

 

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...