tags:

views:

29

answers:

4

I have multiple textboxes which I want them to perform the same thing upon clicking them. By default I can use the handles textbox1.click for 1 single textbox as shown below but I am not sure how to do handle multiples of them. Of course I can write a handler for every single textbox but I have about 50 of them. I am sure there must be a more efficient way. Please advice. Thanks.

   Sub TextBox1_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Click

    If Button9.Text = "Make Changes" Then

        If TextBox2.Text <> "" Then

            Frm_Cine1.Show()
            Frm_Cine1.chooseCine(ComboBox1.SelectedItem)            
        Else
            MsgBox("Please check input!")
            Exit Sub
        End If
    End If
End Sub
+1  A: 

Hi,

If Button9.Text = "Make Changes" Then 

        If TextBox2.Text <> "" Then

These two lines are going to be same for all those 50 buttons?

If yes, then I think you can assign same event handler to each of the button's click event.

Other way is, create one private method which takes one string as an argument and returns boolean value depending on whether your string is blank or not and call this method from all the 50 button's click event.

Shekhar
+1  A: 

why don't you create a customizable textbox?

shaahin
This is also a good option.
Shekhar
A: 

If you indeed need to use the same click handler for multiple test boxes, you can use the AddHandler command to associate the click event of each test box with the handler routine, as shown:

AddHandler TextBoxX.Click AddressOf TextBox1_Click

You will need to add this statement to your program (maybe in the form load routine) once for each text box you want to handle. (Using the name of each text box in place of the "TextBoxX" in the anove code.)

B Pete
Thanks B Pete, thats a lot easier.
A: 

Thanks for all your advices, I am not sure if this is what you guys are suggesting but apparently it is how I wanted it to work:

    Sub TextBoxs_click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles TextBox2.Click, TextBox3.Click, TextBox4.Click 'This part is disturbing if I have 50 textboxes...

            'For Each obj As Control In Panel2.Controls
            If sender.GetType.ToString = "System.Windows.Forms.TextBox" Then
                Dim txtbox As TextBox = sender
                textbox_verification(txtbox)
            End If
            'Next

        End Sub

        Sub textbox_verification(ByVal txtbox As TextBox)

            If Button9.Text = "Make Changes" Then

                If txtbox.Text <> "" Then

                    Frm_Cine1.Show()
                    Frm_Cine1.chooseCine(ComboBox1.SelectedItem, "FILE1-->This should be a variable")
                Else
                    MsgBox("Please check timings input!")
                    Exit Sub
                End If
            End If
        End Sub