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
Solved

Check control focus and change backcolor

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 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