Can you handle all On_Button Events with one Function?

trend

Centurion
Joined
Oct 12, 2004
Messages
171
Hello, I have 23 buttons on my form. 3 of them are for navigation. Then the rest basically will get information put in them from a db... So lets say for example the color spectrum (well 20 colors anyways) are displayed in the form.. one for each button.

How can I say if any of the first 20 buttons are pressed, call AddtoSearchResults(text_of_button_pressed)

right now I am doing it like this :/


Visual Basic:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Call AddtoSearchResults(Button1.Text)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Call AddtoSearchResults(Button2.Text)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Call AddtoSearchResults(button3.Text)
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Call AddtoSearchResults(button4.Text)
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Call AddtoSearchResults(button5.Text)
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Call AddtoSearchResults(button6.Text)
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Call AddtoSearchResults(button7.Text)
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Call AddtoSearchResults(button8.Text)
    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Call AddtoSearchResults(button9.Text)
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Call AddtoSearchResults(button10.Text)
    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Call AddtoSearchResults(button11.Text)
    End Sub

    Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Call AddtoSearchResults(button12.Text)
    End Sub

    Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Call AddtoSearchResults(button13.Text)
    End Sub

    Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Call AddtoSearchResults(button14.Text)
    End Sub

    Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Call AddtoSearchResults(button15.Text)
    End Sub

    Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Call AddtoSearchResults(button16.Text)
    End Sub

    Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Call AddtoSearchResults(button17.Text)
    End Sub

    Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Call AddtoSearchResults(button18.Text)
    End Sub

    Private Sub Button19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Call AddtoSearchResults(button19.Text)
    End Sub

    Private Sub Button20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Call AddtoSearchResults(button20.Text)
    End Sub
 
Last edited by a moderator:
My suggestion would be:
rename Button1_Click(........) to ColorButton_Click(......)
Now you have to let all the 'color' button clicks point to that ColorButton_Click event handler. I don't know the vb syntax for that, but you can use the form designer. The properties window also show the events (a button somewhere on the top of the dialog switches between events and properties). For each color button, select the click event. In the drop down box that appears, select the ColorButton_Click method.

Now you only have 1 event handler that handles all the color button click events, but you havent got the button that send the click event. Luckily, this is stored in the sender object. Cast the sender object to a button (once again, my vb.net knowledge is lacking :( ) and get the text from it. With that text you can call AddtoSearchResults as usuall.
 
Hi Trend,

Put this loop in your form's load event. It loops through the controls in your form and adds a special click handler to them. I am assuming that the textboxes are not in a container control such as a panel or group box. If they are then replace the Me.Controls to the container's name, ie Me.Panel1.Controls.

Visual Basic:
        Dim ctrl As Control
        Dim i As Integer = 1
        For Each ctrl In Me.Controls
            If ctrl.Name = "Button" & i.ToString Then
                AddHandler ctrl.MouseLeave, AddressOf SearchButtons_Click
                If i > 20 Then
                    Exit For
                Else : i += 1
                End If
            End If
        Next

Then add the special handler for the buttons

Visual Basic:
    Private Sub SearchButtons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim txtBox As TextBox = DirectCast(sender, TextBox)
        Call AddtoSearchResults(txtBox.Text)

    End Sub

hope that helps
 
I don't have txtboxes though... Instead of txtboxes I just put the text on the button... so Button1.text = result1from_db

thanks for the help!
Lee
 
Back
Top