[.net] VB.net event handler question

Started by
2 comments, last by Rob Loach 18 years, 5 months ago
Hello all quick question regarding VB.net event handlers. I have a form with 50 text boxes and 5 buttons and a public variable. I hope to find a way to change the text in the text box to the current variable when the box is subject to a double click. for example:


    Private Sub txbxA1_double_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txbxA1.DoubleClick
        txbxA1.Text = Current
    End Sub


    Private Sub txbxA1_double_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txbxB1.DoubleClick
        txbxB1.Text = Current
    End Sub



Now the question is how can I avoid doing this 50 times for each text box, with a coverall event handler, for example:

    Private Sub AnyTextBox_double_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txbxA1.DoubleClick, txbxB1.DoubleClick
        txbx .... whatever one has been clicked = Current
    End Sub


Any ideas would be awesome.
Advertisement
Nevermind i figured it out :) here is the code if anyone has ever has the same problem:

 Private Sub txbxA1_double_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txbxA1.DoubleClick, txbxA2.DoubleClick        Dim textTemp As TextBox        textTemp = CType(sender, TextBox)        textTemp.Text = Current    End Sub


thanks for the view
Hey your solution will give you what you need. The only downfall I see to it is that you would need to manually add handles controlName.DoubleClick, controlName2.DoubleClick, ...

If you're looking to do this for all of the textboxes you could do something like this:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load        For Each tb As TextBox In Me.Controls            AddHandler tb.DoubleClick, AddressOf MyDoubeClick        Next    End Sub    Private Sub MyDoubeClick(ByVal sender As Object, ByVal e As System.EventArgs)        'your code here.    End Sub


This would make MyDoubleClick handle the double click event for all* textboxes on the form. If you didn't want to handle the event for all you could make it a little more complex. You could check the .Name member of the textbox class and do some pattern matching on it. Check to see if the name matches "MyAiTextBox*" with code something like:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load        For Each tb As TextBox In Me.Controls            If (tb.Name Like "MyAiTextBox*") Then _                AddHandler tb.DoubleClick, AddressOf MyDoubeClick        Next    End Sub    Private Sub MyDoubeClick(ByVal sender As Object, ByVal e As System.EventArgs)    End Sub


Anyway just a quick suggestion; good luck!
-Kyle
This is why I hate VB. Thanks for letting me know it is possible!
Rob Loach [Website] [Projects] [Contact]

This topic is closed to new replies.

Advertisement