ddrudik
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.Ente r, ADD_EMPLOYEE_ID_ADMIN.Ente r, ADD_EMPLOYEE_NAME_ADMIN.En ter
m_currentTextBox = CType(sender, TextBox)
USERID_LOGIN.BackColor = Color.White
FILENUMBER_INOUT.BackColor = Color.White
PATRON_INOUT.BackColor = Color.White
ADD_PATRON_ID_ADMIN.BackCo lor = Color.White
ADD_PATRON_NAME_ADMIN.Back Color = Color.White
ADD_EMPLOYEE_ID_ADMIN.Back Color = Color.White
ADD_EMPLOYEE_NAME_ADMIN.Ba ckColor = Color.White
m_currentTextBox.BackColor = System.Drawing.Color.FromA rgb(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.
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,
m_currentTextBox = CType(sender, TextBox)
USERID_LOGIN.BackColor = Color.White
FILENUMBER_INOUT.BackColor
PATRON_INOUT.BackColor = Color.White
ADD_PATRON_ID_ADMIN.BackCo
ADD_PATRON_NAME_ADMIN.Back
ADD_EMPLOYEE_ID_ADMIN.Back
ADD_EMPLOYEE_NAME_ADMIN.Ba
m_currentTextBox.BackColor
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.Ente r, ADD_EMPLOYEE_ID_ADMIN.Ente r, ADD_EMPLOYEE_NAME_ADMIN.En ter
Directcast(sender, TextBox).backcolor = System.Drawing.Color.FromA rgb(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.Leav e, ADD_EMPLOYEE_ID_ADMIN.Leav e, ADD_EMPLOYEE_NAME_ADMIN.Le ave
Directcast(sender, TextBox).backcolor = color.white
End Sub
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,
Directcast(sender, TextBox).backcolor = System.Drawing.Color.FromA
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,
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_GotFocu s(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)
Private Sub ChangeTextboxColor_GotFocu
And just if the .GotFocus or .LostFocus events aren't appropriate use .Enter or .Leave :o)
private sub textbox_gotfocus(...) handles USERID_LOGIN.gotfocus, FILENUMBER_INOUT.gotfocus, ...
dim txt as textbox = ctype(sender, textbox)
txt.backcolor = System.Drawing.Color.FromA rgb(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
dim txt as textbox = ctype(sender, textbox)
txt.backcolor = System.Drawing.Color.FromA
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.
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.
Thanks for the answer.
Sorry about that (cut and paste is always dangerous :-))
Private Sub ChangeTextboxColor_GotFocu
CType(sender, TextBox).BackColor = Color.White
End Sub
Private Sub ChangeTextboxColor_LostFoc
CType(sender, TextBox).BackColor = Color.Blue
End Sub
Then just use them like this:
AddHandler TextBox1.GotFocus, AddressOf ChangeTextboxColor_GotFocu
AddHandler TextBox1.LosttFocus, AddressOf ChangeTextboxColor_LostFoc
And so on...