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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change label color when textbox control receives focu 2

Status
Not open for further replies.

scriv1

MIS
Oct 4, 2001
12
US
Hi folks. I hope someone can help me with code to change the color of a textbox's label when the textbox receives the focus or is activated, and again change the label color back to the original color when the focus leaves the textbox. Thank you for your trouble.
 
Assuming the name of the text box is "TextBox1" and the name of the label is "Label1", use the following code.


Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter
Label1.BackColor = System.Drawing.SystemColors.Desktop
End Sub

Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
Label1.BackColor = System.Drawing.SystemColors.Control
End Sub

I hope this helps.
 
Thank you very much for replying so quickly. I believe that this is the code I need. I will have to wait until I get home to try it, as I am not at my coding machine, but it is very close to what I had been trying before and couldn't get to work right. Thank you again!

Scriv
 
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
 
I very much appreciate the helpful responses that I've received from both of you on this problem. In my case, I only had one textbox and label to worry about so I was able to adapt jerasi's code to do the job for me quite handily.
It worked like a charm.

My adaptation is shown below...


'The next 2 subs change the color of the Clerical label text to red on entering the Clerical textbox, then change it back to black on exiting the textbox.

Private Sub ActivateTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtGeneralClerical.Enter
lblGeneralClerical.ForeColor = Color.Red
End Sub

Private Sub ActivateTextBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtGeneralClerical.Leave
lblGeneralClerical.ForeColor = Color.Black
End Sub


I am going to keep a copy of the responses from both of you for future reference as I know this will come up again and I may be lucky enough to only have to write code one textbox. Again, thank you both very much...

Scriv
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top