• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

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.
0
ddrudik
Asked:
ddrudik
1 Solution
 
RonaldBiemansCommented:
this should be enough

  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
CType(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
CType(sender, TextBox).backcolor =  color.white
    End Sub
0
 
wyoung76Commented:
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...
0
 
RonaldBiemansCommented:
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
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
wyoung76Commented:
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)
0
 
Brian CroweDatabase AdministratorCommented:
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
0
 
wyoung76Commented:
True, Directcast would be better since you already know that only TextBoxes will be used.
0
 
ddrudikAuthor Commented:
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.
0
 
RonaldBiemansCommented:
Sorry about that (cut and paste is always dangerous :-))
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now