Results 1 to 6 of 6

Thread: Function to change back color for label control (solved)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354

    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:
    1. Private Sub txtWeekEnd_LostFocus(ByVal sender As Object, _
    2.         ByVal e As System.EventArgs) _
    3.         Handles txtWeekEnd.LostFocus
    4.  
    5.         lblWeekEnd.BackColor = WhiteSmoke
    6.         lblWeekEnd.ForeColor = Black
    7.  
    8.     End Sub
    9.  
    10.     Private Sub txtEntryPerson_GotFocus(ByVal sender As System.Object, _
    11.         ByVal e As System.EventArgs)
    12.  
    13.         lblEntryPerson.BackColor = Black
    14.         lblEntryPerson.ForeColor = White
    15.  
    16.     End Sub
    17.  
    18.     Private Sub txtEntryPerson_LostFocus(ByVal sender As System.Object, _
    19.         ByVal e As System.EventArgs)
    20.  
    21.         lblEntryPerson.BackColor = WhiteSmoke
    22.         lblEntryPerson.ForeColor = Black
    23.  
    24.     End Sub
    25.  
    26.     Private Sub txtJobNumber_GotFocus(ByVal sender As Object, _
    27.         ByVal e As System.EventArgs) _
    28.         Handles txtJobNumber.GotFocus
    29.  
    30.         lblJobNumber.BackColor = Black
    31.         lblJobNumber.ForeColor = White
    32.  
    33.     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.

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Try something along these lines which assumes the label you want to change is 5 pixels or less above the current textbox.
    VB Code:
    1. Private Sub TextBox2_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.Enter, TextBox3.Enter, TextBox4.Enter, TextBox5.Enter
    2.         Dim txt As TextBox = DirectCast(sender, TextBox)
    3.         'get control 5 pixels above the textbox
    4.         Dim ctrl As Control = Me.GetChildAtPoint(New Point(txt.Location.X, txt.Location.Y - 5))
    5.         ctrl.BackColor = txt.ForeColor
    6.         ctrl.ForeColor = txt.BackColor
    7.     End Sub
    8.  
    9.     Private Sub TextBox2_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.Leave, TextBox3.Leave, TextBox4.Leave, TextBox5.Leave
    10.         Dim txt As TextBox = DirectCast(sender, TextBox)
    11.         'get control 5 pixels above the textbox
    12.         Dim ctrl As Control = Me.GetChildAtPoint(New Point(txt.Location.X, txt.Location.Y - 5))
    13.         ctrl.BackColor = txt.BackColor
    14.         ctrl.ForeColor = txt.ForeColor
    15.     End Sub

  3. #3
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    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:
    1. Private Sub txtName_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtName.Leave
    2.         Call ChangeColor("txtName")
    3. End Sub
    4.  
    5. Public Function ChangeColor(ByVal Name As String)
    6.     Select Case Name
    7.         Case "txtName"
    8.             lblName.BackColor = Black
    9.             lblName.ForeColor = White
    10.     End Select
    11. End Function
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354
    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?

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. 'instead of Handles TextBox2.Leave, TextBox3.Leave, TextBox4.Leave, TextBox5.Leave enter your control names
    2. ... Handles txtWeekEnd.Leave, txtEntryPerson.Leave, txtJobNumber.Leave
    Last edited by Edneeis; Dec 17th, 2003 at 01:38 PM.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354
    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
  •  



Click Here to Expand Forum to Full Width