?
Solved

Changing background of textbox when user enters/exits textbox

Posted on 2005-03-22
8
Medium Priority
?
231 Views
Last Modified: 2012-05-05
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
Comment
Question by:ddrudik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 2000 total points
ID: 13602429
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
 
LVL 4

Expert Comment

by:wyoung76
ID: 13602434
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
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13602436
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 4

Expert Comment

by:wyoung76
ID: 13602465
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
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 13602473
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
 
LVL 4

Expert Comment

by:wyoung76
ID: 13602480
True, Directcast would be better since you already know that only TextBoxes will be used.
0
 
LVL 27

Author Comment

by:ddrudik
ID: 13602572
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
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13602601
Sorry about that (cut and paste is always dangerous :-))
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question