Click here to Skip to main content
13,002,355 members (76,330 online)
Click here to Skip to main content
Add your own
alternative version

Stats

8.2K views
5 bookmarked
Posted 25 Feb 2013

Detect Change In ActiveControl

, 25 Feb 2013
Rate this:
Please Sign up or sign in to vote.
UpdateDefaultButton - Allows detecting change in ActiveControl

Introduction

Occasionally, the topic comes up of how to detect which control in a WinForm has received focus. Most programmers learn that the form property ActiveControl typically (more on this later) points to the control with focus on a given form. You could also use a global event handler attached to all form's controls' GotFocus event as well to gather this information.

However, there is a little known Protected method that has existed in Container Controls (a form is a container control) since .NET 2.0 that is called UpdateDefaultButton. This method is called whenever a containercontrol's ActiveControl property changes. This change typically is the result of either a mouse or keyboard action, but it may also be set programmatically.

This method can be used as a global GotFocus event handler as long as you do not programmatically set the ActiveControl property to a non-focusable control.

Using the Code

How you use this information is up to you. Here is one possible implementation.
In this, I have defined a custom event named FocusChanged and a custom event argument for the event. The event is raised when form calls the UpdateDefaultButton method.

Friend Class FocusChangedArg
   Inherits EventArgs
   Public LastControlWithFocus As Control
   Public Sub New(ByVal Last As Control)
      LastControlWithFocus = Last
   End Sub
End Class

Private LastActiveControl As Control = Me
Friend Event FocusChanged As EventHandler(Of FocusChangedArg)

Protected Overrides Sub UpdateDefaultButton()
    RaiseEvent FocusChanged(Me, New FocusChangedArg(LastActiveControl))
    LastActiveControl = Me.ActiveControl ' Store this for the next time Focus changes
End Sub

Private Sub Form1_FocusChanged(ByVal sender As Object, _
 ByVal e As FocusChangedArg) Handles Me.FocusChanged
    'do something
End Sub

Points of Interest

One thing that I discovered is that UpdateDefaultButton is called before the Click or DoubleClick events are fired. Therefore, it is possible to attached their respective handlers in the UpdateDefaultButton method and if the ActiveControl changed due to a mouse click, these handlers will then be called.

Protected Overrides Sub UpdateDefaultButton()
   RaiseEvent FocusChanged(Me, New FocusChangedArg(LastActiveControl))

   If LastActiveControl IsNot Nothing Then
      RemoveHandler LastActiveControl.Click, AddressOf anycontrol_Click
      RemoveHandler LastActiveControl.DoubleClick, AddressOf anycontrol_DoubleClick
   End If
   LastActiveControl = Me.ActiveControl ' Store this for the next time Focus changes

   AddHandler LastActiveControl.Click, AddressOf anycontrol_Click
   AddHandler LastActiveControl.DoubleClick, AddressOf anycontrol_DoubleClick

End Sub

Private Sub anycontrol_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  ' do something
End Sub

Private Sub anycontrol_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
  'do something
End Sub

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0

Share

About the Author

TnTinMn
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170626.1 | Last Updated 25 Feb 2013
Article Copyright 2013 by TnTinMn
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid