tags:

views:

82

answers:

1

Using VB 2008 Express.

Attaching code to buttons when they're clicked is easy when they're static buttons on the form; just double click the button on the form designer and add code to the event.

I have a series of buttons that are generated in a control array, so they're generated in a class, and in the form there are no buttons until runtime.

So newbie question with a probably simple answer...how do I enter code for the click event for buttons that aren't there until they're instantiated as a class at runtime?

A: 

The way you would do it for regular buttons, I think.

What action do you want to take on each of the buttons? If the action is different for each of the button, could you give an example of what it looks like?

EDIT: Crude code ahead

   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim button As Button()
        ReDim button(2)

        Dim button1 As New Button
        button1.Top = 0
        button1.Height = 100
        button1.Text = "hello"

        Dim button2 As New Button
        button2.Top = 200
        button2.Height = 100
        button2.Text = "world"

        button(0) = button1
        button(1) = button2

        For i As Integer = 0 To 1
            '** This is where all the buttons are tied, to a common handler
            AddHandler button(i).Click, AddressOf doSomething
        Next

        Me.Controls.AddRange(button)
    End Sub


    Protected Sub doSomething(ByVal sender As Object, ByVal e As EventArgs)
        Dim thisButton As Button
        thisButton = sender

        thisButton.BackColor = Color.DarkBlue
        thisButton.Text = "clicked"
    End Sub
shahkalpesh
When clicked, I want to toggle the button to a different background and foreground color, and change the btn(i).text property as well. Same actions on each of the buttons (it's a grid to toggle them between "normal" and "selected"). I'm just unclear on how to know where to put code for intercepting when btn(i) is clicked and then run the code to change or toggle the properties since it's not in the design tab for the form.
Bart Silverstrim
@shahkalpesh re: edit-your code is essentially adding an event handler called button(i).click to a subroutine called doSomething, then I'd create the subroutine to do what I want it to do, and both of these would be part of the class definition, yes? (making sure I understand what's happening...I want to learn and not just parrot the code to make it simply work :-) I also might need to google more info on "addhandler" to customize it, I think.
Bart Silverstrim
Yes. `doSomething` is tied to all the button's click event. Read about events/delegates in .net. You will find plenty of good example of it on the net/stackoverflow.
shahkalpesh
@shahkalpesh: Thanks for the info! I'll try it out when I get a chance. Hopefully this will give me the nudge in the right direction to finding the solution that works in my hodgepodge of a program :-)
Bart Silverstrim
Person that was going to use the program decided they didn't want the capability. I'm going to try it out later anyway, though. Seeing as you're the only person that took the time to help this newbie out with this question (and from what I googled afterwards this looks like the way to go), I'll give it the accept and give you another thank you! I appreciate it!
Bart Silverstrim
Glad to be of help.
shahkalpesh