[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 575
  • Last Modified:

Tracking the current control Focus in WinForms


Hi,

I would like to know if it is possible to perform the following activity within a WinForm using Vb.net 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?

Regards,


Alan
0
alan_ITG
Asked:
alan_ITG
  • 3
1 Solution
 
Fernando SotoCommented:
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

        AttachHandler(Me)

    End Sub

    Private Sub AttachHandler(ByVal cc As Control)

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

    End Sub

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

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

    End Sub


Fernando
0
 
jrschererCommented:
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!  Jack.net
(alan ITG: no points to me!)
0
 
alan_ITGAuthor Commented:

Thanks Fernando & Jack.

As requested by Jack all points to Fernando.

The code works beautifully.
0
 
Fernando SotoCommented:
Glad I was able to help. :=)
0
 
Fernando SotoCommented:
Thanks Jack.net good point.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now