views:

1699

answers:

2

How do I find out which control has focus in winforms?

+7  A: 

Is Form.ActiveControl what you want?

Ken Browning
+1  A: 

Something along these lines:

Protected Function GetFocusControl() As Control
    Dim focusControl As Control = Nothing

    ' Use this to get the Focused Control: 
    Dim focusHandle As IntPtr = GetFocus()
    If IntPtr.Zero.Equals(focusHandle) Then          
      focusControl = Control.FromHandle(focusHandle)
    End If

    ' Note that it returns NOTHING if there is not a .NET control with focus 
    Return focusControl
End Function

I think this code came from windowsclient.net, but it's been a while so...

Stephen Wrighton