Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

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 :/

 

 

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

Edited by PlausiblyDamp
Posted

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.

Nothing is as illusive as 'the last bug'.
  • *Experts*
Posted

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.

 

       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

 

   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

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

Posted

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

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