|
-
Dec 17th, 2003, 12:24 PM
#1
Thread Starter
Hyperactive Member
Function to change back color for label control (solved)
I have a series of text boxes that the user tabs through. Right now have a lot of redundant code that I think I could put in one function if I knew how.
Basically what I have going on is that when a user is in a text control I reverse the colors on the label control for that text box. After the user leaves the text box its label goes back to the original color.
Here is a sample of the code I have now.
VB Code:
Private Sub txtWeekEnd_LostFocus(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles txtWeekEnd.LostFocus
lblWeekEnd.BackColor = WhiteSmoke
lblWeekEnd.ForeColor = Black
End Sub
Private Sub txtEntryPerson_GotFocus(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
lblEntryPerson.BackColor = Black
lblEntryPerson.ForeColor = White
End Sub
Private Sub txtEntryPerson_LostFocus(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
lblEntryPerson.BackColor = WhiteSmoke
lblEntryPerson.ForeColor = Black
End Sub
Private Sub txtJobNumber_GotFocus(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles txtJobNumber.GotFocus
lblJobNumber.BackColor = Black
lblJobNumber.ForeColor = White
End Sub
How do I put that in a function to reduce the amount of code?
I kinda have an understanding of the use of functions but I am having some problem getting it to work for the current text box only (the calling text box).
Last edited by BukHix; Dec 17th, 2003 at 02:40 PM.
-
Dec 17th, 2003, 12:44 PM
#2
Try something along these lines which assumes the label you want to change is 5 pixels or less above the current textbox.
VB Code:
Private Sub TextBox2_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.Enter, TextBox3.Enter, TextBox4.Enter, TextBox5.Enter
Dim txt As TextBox = DirectCast(sender, TextBox)
'get control 5 pixels above the textbox
Dim ctrl As Control = Me.GetChildAtPoint(New Point(txt.Location.X, txt.Location.Y - 5))
ctrl.BackColor = txt.ForeColor
ctrl.ForeColor = txt.BackColor
End Sub
Private Sub TextBox2_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.Leave, TextBox3.Leave, TextBox4.Leave, TextBox5.Leave
Dim txt As TextBox = DirectCast(sender, TextBox)
'get control 5 pixels above the textbox
Dim ctrl As Control = Me.GetChildAtPoint(New Point(txt.Location.X, txt.Location.Y - 5))
ctrl.BackColor = txt.BackColor
ctrl.ForeColor = txt.ForeColor
End Sub
-
Dec 17th, 2003, 12:48 PM
#3
Frenzied Member
What about creating a function that takes the name of the control then does a select case to determine the color of the label?
Something like this
VB Code:
Private Sub txtName_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtName.Leave
Call ChangeColor("txtName")
End Sub
Public Function ChangeColor(ByVal Name As String)
Select Case Name
Case "txtName"
lblName.BackColor = Black
lblName.ForeColor = White
End Select
End Function
Being educated does not make you intelligent.
Need a weekend getaway??? Come Visit
-
Dec 17th, 2003, 01:00 PM
#4
Thread Starter
Hyperactive Member
Thanks for the replies. I was hoping I could cut a lot of the code down or remove the need to know the control names. The function that Memnoch1207 is pretty close to what I was trying on my own.
Is it possible to have Case "txtName" be a variable depending on the text box that sent it?
-
Dec 17th, 2003, 01:17 PM
#5
The two methods I posted should cover all of the controls. The only place that needs the name is adding them to the event handler.
VB Code:
'instead of Handles TextBox2.Leave, TextBox3.Leave, TextBox4.Leave, TextBox5.Leave enter your control names
... Handles txtWeekEnd.Leave, txtEntryPerson.Leave, txtJobNumber.Leave
Last edited by Edneeis; Dec 17th, 2003 at 01:38 PM.
-
Dec 17th, 2003, 02:40 PM
#6
Thread Starter
Hyperactive Member
Thanks Edneeis I got it now.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|