Create a form and call it something like BaseForm. Don't put any controls
on it.
Put this in the code behind the form:
Private Sub AddEventHandlers(ByVal ctrlContainer As Control)
For Each ctrl As Control In ctrlContainer.Controls
If TypeOf ctrl Is TextBox _
OrElse TypeOf ctrl Is ComboBox Then
AddHandler ctrl.Enter, AddressOf ProcessEnter
AddHandler ctrl.Leave, AddressOf ProcessLeave
End If
'If control has children, call this function recursively
If ctrl.HasChildren Then
AddEventHandlers(ctrl)
End If
Next
End Sub
Private Sub ProcessEnter(ByVal sender as Object, ByVal e as
System.EventArgs)
DirectCast(sender, Control).BackColor = Color.Lavender
End Sub
Private Sub ProcessLeave(ByVal sender as Object, ByVal e as
System.EventArgs)
DirectCast(sender, Control).BackColor =
Color.FromKnownColor(KnownColor.Window)
End Sub
Add the BaseForm_Load event and put this in there:
AddEventHandlers(me)
Then create your new form, and either change it to inherit from BaseForm
(this is probably in the designer-generated code), or use the Inheritance
Picker to create your new form (which does the same thing).
Put your textboxes on your new form, and run it, and voila! You get colors
when you enter a textbox, and back to the original color when they leave.
If you have every form inherit from the BaseForm, it will work the same on
every form with no further effort on your part.
Robin S.