Electron Posted March 27, 2003 Posted March 27, 2003 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 Quote
*Gurus* divil Posted March 27, 2003 *Gurus* Posted March 27, 2003 Is the problem that you need to establish in the click procedure which button raised the event? Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Electron Posted March 28, 2003 Author Posted March 28, 2003 Yes, I need to find a way to find out which button was clicked. Quote
Heiko Posted March 28, 2003 Posted March 28, 2003 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. Quote .nerd
Gizmo001 Posted April 3, 2003 Posted April 3, 2003 (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 April 3, 2003 by divil Quote
Guest mutant Posted April 3, 2003 Posted April 3, 2003 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.