Just to add to this thread:
If you had more than 2 textboxes though, this could become quite cumbersome code wise (if you had 10 text boxes, you'd need a textbox_enter and textbox_leave for each of them! Thats 20 subs!)
Here's another solution:
Private Function SwitchColor(ByRef objectForm As Form, ByVal stringLabel As String, ByVal boolFocus As Boolean)
Dim label As New Label()
Dim i As Integer
For i = 0 To objectForm.Controls.Count - 1
If objectForm.Controls(i).Name = stringLabel Then
label = objectForm.Controls(i)
If boolFocus Then
label.BackColor = Color.Yellow
Else
label.BackColor = Color.Gray
End If
Exit For
End If
Next
End Function
Private Sub Text_GetFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter, TextBox2.Enter
SwitchColor(Me, sender.Tag, True)
End Sub
Private Sub Text_LoseFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave, TextBox2.Leave
SwitchColor(Me, sender.Tag, False)
End Sub
Here's how it works. With .NET, a single sub can handle multiple events. So instead of a seperate enter/leave sub for each textbox, we just write two subs, and at the end include all the textbox's that we want these subs to handle (hence the TExtBox1.Enter, TextBox2.Enter).
The SwitchColor takes three parameters:
Me: this represents the form
sender.Tag: the sender in this case is the textbox. I set the Tag property to be the name of the corresponding label.
True/False: telling it whether this is an Entry or Leave
The SwitchColor function does the rest: finds the matching label, and sets the color to whatever color you specify in the code.
hth
D'Arcy