How do I find out which control has focus in .NET Windows Forms?

前端 未结 7 1218
逝去的感伤
逝去的感伤 2020-12-03 21:06

How do I find out which control has focus in Windows Forms?

7条回答
  •  无人及你
    2020-12-03 21:24

    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...

提交回复
热议问题