Sign in to follow this  

[.net] VB.net event handler question

This topic is 4411 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

This topic is 4411 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this