Jump to content
Xtreme .Net Talk

Code behind a button created with code


Recommended Posts

Guest Notbob
Posted
I used code to create an array of buttons that show up on the screen. Now, when someone clicks on those buttons I want to know how to run the code behind it. In my program the buttons are cmdSelect(20) 20 being the array size. When they click on the 5th button I want it to say in a text box, you clicked on button 5. If someone can drive me there, I can figure out the rest of the trip. THANKS IN ADVANCE.
  • *Gurus*
Posted (edited)

When you create the buttons, assign their Tag property their index number in the array. Also, as you create them, you need to wire up their Click events:

 

AddHandler btnArray(index).Click, AddressOf myEventFunction

 

Then you create your event handler function:

Private Sub myEventFunction(ByVal sender As System.Object, ByVal e As System.EventArgs)
   Dim btnSender As Button
   Dim intIndex As Integer

   btnSender = CType(sender, Button)
   intIndex = CType(btnSender.Tag, Integer)

   MsgBox(intIndex)
End Sub

Edited by divil

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Guest Notbob
Posted

confused

 

Where do I add that addhandler line

Guest Notbob
Posted

add handler

 

But everytime I type that line, it underlines the addhanlder and says syntax error. I copied the exact text you orginally posted.

Guest Notbob
Posted

code for runtime buttons

 

Ok, I am not getting this at all. Maybe I need to back up and explain. In VB 6 I used to be able to take a button and draw it right onto the form. I would then copy it and paste it to the form. I was then asked to make an array of that button. In vb.net it just adds another button and calls it button2.

 

So found a procedure to adds the buttons through the code in an array. The problem is, when I click on those buttons nothing happens. When I stop the program to edit the code, there is no button for me to double click on to edit the code like I used to in vb6. My question is, when I click on these multiple buttons that are created at run time, what code is it accessing, and how do I edit it. I sort of understand the idea behind the addhandler thing, but it does not seem to recognize the array of buttons.

Guest Notbob
Posted

Thanks

 

That was very helpful and sort of pulled it all together for me. Thanks for everyone's help. I got my button to work with your help. With this code I plan to take over the world. Thanks

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