Guest Notbob Posted July 30, 2002 Posted July 30, 2002 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. Quote
*Gurus* divil Posted July 30, 2002 *Gurus* Posted July 30, 2002 (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 July 30, 2002 by divil 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
Guest Notbob Posted July 30, 2002 Posted July 30, 2002 confused Where do I add that addhandler line Quote
*Gurus* divil Posted July 30, 2002 *Gurus* Posted July 30, 2002 Just after you've created each one 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
Guest Notbob Posted July 30, 2002 Posted July 30, 2002 add handler But everytime I type that line, it underlines the addhanlder and says syntax error. I copied the exact text you orginally posted. Quote
*Gurus* Thinker Posted July 30, 2002 *Gurus* Posted July 30, 2002 Where are you putting the code? divil's code works ok for me. Quote Posting Guidelines
Guest Notbob Posted July 30, 2002 Posted July 30, 2002 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. Quote
*Gurus* Thinker Posted July 30, 2002 *Gurus* Posted July 30, 2002 No, it isn't going to recognize the whole array. You have to do the AddHandler for each member of the array. Quote Posting Guidelines
Guest TheIrishCoder Posted July 31, 2002 Posted July 31, 2002 http://www.developerfusion.com/show/2499/ Article explaining how to deal with programmatic creation and handling of multiple contorls. I wrote this to be as simple as possible and included a sample project. Quote
*Gurus* divil Posted August 1, 2002 *Gurus* Posted August 1, 2002 Maybe we should put that in a sticky thread somewhere since this same question seems to get asked in a different way every day. 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
Guest Notbob Posted August 2, 2002 Posted August 2, 2002 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 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.