Reputation: 11
I was wondering if there is any way to combine events in VB.net. I am using a windows form and have three events for each button that does a different action. I would like to put all three events in one method but am having trouble doing this. Below is an example of one of the buttons. For the ease of the question I removed all the code inside each sub.
Private Sub btnRightPos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRightPos.Click
End Sub
Private Sub btnRightPos_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnRightPos.MouseDown
End Sub
Private Sub btnRightPos_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnRightPos.MouseUp
End Sub
Upvotes: 1
Views: 2424
Reputation: 11773
This seems to be the prudent approach. If there is something just a little different in one it is easier to fix. Just because they are identical today...
Private Sub btnRightPos_Click(sender As Object, _
ByVal e As System.EventArgs) _
Handles btnRightPos.Click
btnRightPosCommon(sender, e, "C")
End Sub
Private Sub btnRightPos_MouseDown(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles btnRightPos.MouseDown
btnRightPosCommon(sender, e, "D")
End Sub
Private Sub btnRightPos_MouseUp(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles btnRightPos.MouseUp
btnRightPosCommon(sender, e, "U")
End Sub
'common
Private Sub btnRightPosCommon(ByVal sender As System.Object, _
ByVal e As System.Object, Optional id As String = "")
Debug.WriteLine("{0} {1} {2}", sender.GetType, e.GetType, id)
End Sub
Upvotes: -2
Reputation: 141638
It depends. Do you care about the arguments? Do you need sender
and e
? You can combine two of them, and if you don't care about the arguments, you can combine all three. The reason being the signature for Click is different. Since Up and Down share the same signature, they could be combined like this:
Private Sub btnRightPos_UpAndDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnRightPos.MouseDown, btnRightPos.MouseUp
End Sub
If want to combine all three:
Private Sub btnRightPos_All() Handles btnRightPos.Click, btnRightPos.MouseUp, btnRightPos.MouseDown
End Sub
VB.NET has an interesting feature that lets that work, where you can use a parameter-less method to handle any event; effectively saying "I don't care about the signature, make it work".
Upvotes: 2
Reputation: 16575
You can easily achieve this using
AddHandler obj.OnEvent, AddressOf MyEventHandler
So for example
private Default_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
and then
AddHandler btnRightPos.MouseUp, AddressOf Default_MouseUp
AddHandler btnLeftPos.MouseUp, AddressOf Default_MouseUp
Upvotes: 1
Reputation: 108957
MouseUp
and MouseDown
can be combined by saying Handles btnRightPos.MouseDown, btnRightPos.MouseUp
like this.
Private Sub btnRightPos_MouseDownAndDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnRightPos.MouseDown, btnRightPos.MouseUp
End Sub
but Button.Click cannot be combined with the other two as it's event handler has a different signature.
On thing you can do to reuse code is to seperate out the common logic into a separate function and just call that function from your event handlers.
Upvotes: 4