Solved

Check control focus and change backcolor

Posted on 2009-05-05
2
297 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 500 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…

734 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