?
Solved

Check control focus and change backcolor

Posted on 2009-05-05
2
Medium Priority
?
300 Views
Last Modified: 2012-05-06
Hello Experts,

I was wondering if there is a easy way of doing this.

I would like to change the BackColor of the control on a windows Form that has the focus. When the control has the Focus, I would like the BackColor to be say Yellow; when the control looses focus I would like the BackColor to go back to White.

I know I could use the GotFocus event of each control but If I have 20 controls on a form that would not be a good pratice to do like that.

Thanks for your sugestions.

jppinto
0
Comment
Question by:jppinto
[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
2 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 24309457
Try something like this...
Public Class Form1
 
    Private PrevBackColor As Color
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        WireControls(Me)
    End Sub
 
    Private Sub WireControls(ByVal cont As Control)
        For Each ctl As Control In cont.Controls
            Try
                AddHandler ctl.GotFocus, AddressOf ctl_GotFocus
                AddHandler ctl.LostFocus, AddressOf ctl_LostFocus
            Catch ex As Exception
            End Try
            If ctl.HasChildren Then
                WireControls(ctl)
            End If
        Next
    End Sub
 
    Private Sub ctl_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim ctl As Control = CType(sender, Control)
        PrevBackColor = ctl.BackColor
        ctl.BackColor = Color.Yellow
    End Sub
 
    Private Sub ctl_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim ctl As Control = CType(sender, Control)
        ctl.BackColor = PrevBackColor
    End Sub
 
End Class

Open in new window

0
 
LVL 33

Author Comment

by:jppinto
ID: 24309609
Hello Idle_Mind,

Your solution is perfect (as always!). Thanks,

jppinto
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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
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…

719 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