Solved

Check control focus and change backcolor

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 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

19 Experts available now in Live!

Get 1:1 Help Now