In this case, you can just use sender
. The sender
parameter is a reference to whichever control is raising the event. So, if you add this same method as an event handler for multiple controls, sender
will be which ever control raised the event that it's currently handling, for instance:
Private Sub MouseDown(sender As Object, e As EventArgs) _
Handles TextBox1.MouseDown, TextBox2.MouseDown
' Note in the line above that this method handles the event
' for TextBox1 and TextBox2
Dim textBox As TextBox = CType(sender, TextBox)
' textBox will now be either TextBox1 or TextBox2, accordingly
textBox.Capture = False
' ....
End Sub
The CType
statement casts the base Object
parameter to the specific TextBox
class. In this example, the method only handles events for TextBox
objects, so that will work. However, if you have it handle events from other types of controls, you'd need to cast to the more general Control
type (i.e. CType(sender, Control)
).