Tracking the current control Focus in WinForms

Posted on 2006-04-29
Last Modified: 2011-09-20


I would like to know if it is possible to perform the following activity within a WinForm using 2005.

1) Track the users movement through the form controls (via Tab or mouse).
2) On entering the control (gotfocus) use the value stored in the TAG property to update a statusbar label.

I know that I can write GotFocus event handlers for all the controls on the form, but wanted to implement a global approach if it is possible.

Any ideas?


Question by:alan_ITG
    LVL 62

    Accepted Solution

    Hi alan_ITG;

    You can attach all of the controls GotFocus events to one subroutine and have that write to the status bar.

        Private Sub Form1_Load(ByVal sender As Object, _
            ByVal e As System.EventArgs) Handles Me.Load


        End Sub

        Private Sub AttachHandler(ByVal cc As Control)

            For Each ctl As Control In cc.Controls
                If ctl.Controls.Count > 0 Then
                    AddHandler ctl.GotFocus, AddressOf Control_GotFocus
                End If

        End Sub

        Private Sub Control_GotFocus(ByVal sender As Object, _
            ByVal e As System.EventArgs)

            ToolStripStatusLabel1.Text = CType(sender, Control).Tag.ToString()

        End Sub

    LVL 9

    Expert Comment

    Hi Fernando,
    A beautyful use of recursive programming!

    You can potentially increasing the efficiency a bit with the addition below, since there could be a lot of unselectable crab on the screen (like in my cases)

    If ctl.CanSelect Then AddHandler ctl.GotFocus, AddressOf Control_GotFocus

    Also important to say that dynamically added selectable controls will need the same AddHandler pointer at time of addition.

    I like this code!
    (alan ITG: no points to me!)

    Author Comment


    Thanks Fernando & Jack.

    As requested by Jack all points to Fernando.

    The code works beautifully.
    LVL 62

    Expert Comment

    by:Fernando Soto
    Glad I was able to help. :=)
    LVL 62

    Expert Comment

    by:Fernando Soto
    Thanks good point.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    729 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

    18 Experts available now in Live!

    Get 1:1 Help Now