Link to home
Start Free TrialLog in
Avatar of ddrudik
ddrudikFlag for United States of America

asked on

Changing background of textbox when user enters/exits textbox

I am currently using this code:

Private m_currentTextBox As TextBox

    Private Sub TrackTextBoxFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles USERID_LOGIN.Enter, FILENUMBER_INOUT.Enter, PATRON_INOUT.Enter, ADD_PATRON_ID_ADMIN.Enter, ADD_PATRON_NAME_ADMIN.Enter, ADD_EMPLOYEE_ID_ADMIN.Enter, ADD_EMPLOYEE_NAME_ADMIN.Enter
        m_currentTextBox = CType(sender, TextBox)
        USERID_LOGIN.BackColor = Color.White
        FILENUMBER_INOUT.BackColor = Color.White
        PATRON_INOUT.BackColor = Color.White
        ADD_PATRON_ID_ADMIN.BackColor = Color.White
        ADD_PATRON_NAME_ADMIN.BackColor = Color.White
        ADD_EMPLOYEE_ID_ADMIN.BackColor = Color.White
        ADD_EMPLOYEE_NAME_ADMIN.BackColor = Color.White
        m_currentTextBox.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(255, Byte), CType(192, Byte))
    End Sub

I am looking to make this code more efficient since I have about 200 of these textboxes (across 10 tabs in 1 tab control) to set this for.  Best case I would like to have code that when I entered any textbox it made that textbox background a certain color and didn't modify any other textboxes, then when I exited that textbox it set it's background color back to white.

Thanks in advance for any assistance.
ASKER CERTIFIED SOLUTION
Avatar of RonaldBiemans
RonaldBiemans

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Create a handler which you reference for every Textbox

  Private Sub ChangeTextboxColor_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs)
    CType(sender, TextBox).BackColor = Color.White
  End Sub

  Private Sub ChangeTextboxColor_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs)
    CType(sender, TextBox).BackColor = Color.Blue
  End Sub

Then just use them like this:

  AddHandler TextBox1.GotFocus, AddressOf ChangeTextboxColor_GotFocus
  AddHandler TextBox1.LosttFocus, AddressOf ChangeTextboxColor_LostFocus

And so on...
Avatar of RonaldBiemans
RonaldBiemans

although this would be better

  Private Sub TrackTextBoxFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles USERID_LOGIN.Enter, FILENUMBER_INOUT.Enter, PATRON_INOUT.Enter, ADD_PATRON_ID_ADMIN.Enter, ADD_PATRON_NAME_ADMIN.Enter, ADD_EMPLOYEE_ID_ADMIN.Enter, ADD_EMPLOYEE_NAME_ADMIN.Enter
Directcast(sender, TextBox).backcolor =  System.Drawing.Color.FromArgb(CType(255, Byte), CType(255, Byte), CType(192, Byte))
    End Sub

  Private Sub TrackTextBoxFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles USERID_LOGIN.Leave, FILENUMBER_INOUT.Leave, PATRON_INOUT.Leave, ADD_PATRON_ID_ADMIN.Leave, ADD_PATRON_NAME_ADMIN.Leave, ADD_EMPLOYEE_ID_ADMIN.Leave, ADD_EMPLOYEE_NAME_ADMIN.Leave
Directcast(sender, TextBox).backcolor =  color.white
    End Sub
You could also just add all the events on the end of the handler like RonaldBiemans has suggested in his solution too.

  Private Sub ChangeTextboxColor_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles [and here you can add all 200 TextBox events]

And just if the .GotFocus or .LostFocus events aren't appropriate use .Enter or .Leave  :o)
Avatar of Brian Crowe
private sub textbox_gotfocus(...) handles USERID_LOGIN.gotfocus, FILENUMBER_INOUT.gotfocus, ...
dim txt as textbox = ctype(sender, textbox)
txt.backcolor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(255, Byte), CType(192, Byte))
end sub

private sub textbox_lostfocus(...) handles USERID_LOGIN.lostfocus, FILENUMBER_INOUT.lostfocus, ...
dim txt as textbox = ctype(sender, textbox)
txt.backcolor = color.white
end sub
True, Directcast would be better since you already know that only TextBoxes will be used.
Avatar of ddrudik

ASKER

RonaldBiemans, the two subs worked well, the only minor change I would recommend for others who use the code from this answer that you will need to change the name of the second Private Sub something other than TrackTextBoxFocus since that name was used for the first Private Sub in the example.

Thanks for the answer.
Sorry about that (cut and paste is always dangerous :-))