Solved

Check control focus and change backcolor

Posted on 2009-05-05
2
293 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
2 Comments
 
LVL 85

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now