Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Obtaining reference to Control in its Event Handler

Status
Not open for further replies.

infimo

Technical User
Apr 8, 2004
22
US
Hi!

How do you obtain a reference to a control from its event handler without knowing the name of that control?

On my form I have some textboxes that have the same code in all their LostFocus and GotFocus events. The only difference is the name of that control itself.

I was wondering if I could create a base textbox with these event handlers coded in and make all my textboxes inherit this textbox... That would make my code much shorter.

Thanks in advance.

Mohit
 
just inherit the control

and then make the lostfocus and gotfocus event

and use the me or mybase keyword to refer to the control itself. the compiler will know what to do.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
What a wonderfull world - Louis armstrong
 
Infimo,

You do not need to inherit anything.

By convention, the first paramater passed to every event is the sender, the object that raised the event.

Private Sub txtFormatString_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles txtFormatString.TextChanged, txtFormat2.TextChanged
' Treat sender as a TextBox
Dim txt As TextBox = DirectCast(eventSender, TextBox)
Dim txtName As String = txt.Name
......
End Sub

Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
Wow!!

Thats just what I was looking for JohnYingling!!

Thanks a lot.

Mohit
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top