mass addhandler help

Chaseshaw

Member
Joined
Apr 21, 2010
Messages
8
Programming Experience
Beginner
I have a form with ~70 textboxes, and I would like to add a sub to run whenever one of then gets focus. I have:

For Each tb In Me.Controls
If TypeOf tb Is TextBox Then
AddHandler tb.GotFocus, AddressOf tb_gotfocus
End If
Next

but it says "'GotFocus' is not an event of 'Object'". How do i do this en masse like this so it works? thanks.
 
For example:
VB.NET:
For Each tb [U]As Control[/U] In Me.Controls
Now tb variable is typed Control and you can access all members of Control class.
You should in most cases use the Enter event instead of GotFocus (explains).

If you need to access TextBox specific members you can cast the Control object using CType/DirectCast.
 
Back
Top