Solved

Check control focus and change backcolor

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

770 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