Is there a way to determine is any control at all on a form has gotten focus? I know you can individually determine if a single control has focus or has been clicked, etc. but is there an event that can tell you that some/any control has been clicked?

i.e.

Private Sub myFrm_Magic(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.SomethingClicked

  Msgbox(Me.ActiveControl.Name)

End Sub
Comments
I guess you figured it out on your own.

Not without adding a handler to all of them.

Public Class Form1

    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        ' Add a click event to all controls
        AddClickEventHandlers(Me)

    End Sub

    Private Sub AddClickEventHandlers(ByVal control As Control)
        'Recursivly add a handler to all controls
        AddHandler control.Click, AddressOf Control_Click

        For i = 0 To control.Controls.Count - 1
            AddClickEventHandlers(control.Controls(i))
        Next

    End Sub

    Private Sub RemoveClickEventHandlers(ByVal control As Control)

        RemoveHandler control.Click, AddressOf Control_Click

        For i = 0 To control.Controls.Count - 1
            RemoveClickEventHanglers(control.Controls(i))
        Next

    End Sub

    Private Sub Control_Click(ByVal sender As Object, ByVal e As EventArgs)
        MsgBox(CType(sender, Control).Name)
    End Sub

    Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        RemoveClickEventHandlers(Me)
    End Sub

End Class

Edited 5 Years Ago by Unhnd_Exception: n/a

This article has been dead for over six months. Start a new discussion instead.