24

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jeff
  • 8,020
  • 34
  • 99
  • 157

7 Answers7

31

Form.ActiveControl may be what you want.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ken Browning
  • 28,693
  • 6
  • 56
  • 68
  • 1
    Just want to point out that if you have changed something (set a .Text property for example) it will return the control that you last used. including setting things like the .Text not always the control that has focus – PsychoData Apr 07 '14 at 13:55
14

Note that a single call to ActiveControl is not enough when hierarchies are used. Imagine:

Form
    TableLayoutPanel
        FlowLayoutPanel
            TextBox (focused)

(formInstance).ActiveControl will return reference to TableLayoutPanel, not the TextBox

So use this (full disclosure: adapted from this C# answer)

  Function FindFocussedControl(ByVal ctr As Control) As Control
    Dim container As ContainerControl = TryCast(ctr, ContainerControl)
    Do While (container IsNot Nothing)
      ctr = container.ActiveControl
      container = TryCast(ctr, ContainerControl)
    Loop
    Return ctr
  End Function
Community
  • 1
  • 1
MarkJ
  • 30,070
  • 5
  • 68
  • 111
  • Sample use: Private Sub frmPartes_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown If e.KeyCode = Keys.Enter Then Dim Ct As Windows.Forms.Control Ct = FindFocussedControl(Me.ActiveControl) If TypeOf (Ct) Is TextBox Or TypeOf (Ct) Is System.Windows.Forms.UserControl Then Filtrar() e.Handled = True e.SuppressKeyPress = True End If End If End Sub maybe are necesisary use this: Windows.Forms.Control instead only control – R.Alonso Jul 08 '21 at 08:19
  • 1
    This should be the correct answer. ActiveControl alone doesn't do what you need every time (which is what sent me looking here!). – Dan Jan 30 '23 at 19:18
3

You can use the ActiveControl propert of the form and can use that control.

me.ActiveControl

Or

Form.ActiveControl
kleopatra
  • 51,061
  • 28
  • 99
  • 211
3

In C# I do this:

        if (txtModelPN != this.ActiveControl)
            txtModelPN.BackColor = Color.White;

txtModelPN is a textbox that I am highlighting on enter and mouseEnter and de-highlighting on Leave,MouseLeave. Except if it is the current control I don't set the background back to white.

The VB equivalent would be like this

IF txtModelPN <> Me.ActiveControl Then
   txtModelPN.BackColor = Color.White
End If
xDJR1875
  • 31
  • 1
1

You can use this to find by Control Name .

    If DataGridView1.Name = Me.ActiveControl.Name Then
        TextBox1.Visible = True
    Else
        TextBox1.Visible = False
    End If
KHALID
  • 87
  • 7
0

I used following:

Private bFocus = False
Private Sub txtUrl_MouseEnter(sender As Object, e As EventArgs) Handles txtUrl.MouseEnter
    If Me.ActiveControl.Name <> txtUrl.Name Then
        bFocus = True
    End If
End Sub

Private Sub txtUrl_MouseUp(sender As Object, e As MouseEventArgs) Handles txtUrl.MouseUp
    If bFocus Then
        bFocus = False
        txtUrl.SelectAll()
    End If
End Sub

I set the Variable only on MouseEnter to improve the magic

-1

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
  • 36,783
  • 6
  • 67
  • 86
  • 2
    Please let me downvote the answer until you complete it. The call of `GetFocus()` hangs in nowhere and the answer in this form simply won't work. – miroxlav Nov 10 '14 at 18:44
  • Upvote - while this does require P/Invoke and the answer doesn't fully explain how to call the function, a bit more searching for ".NET GetFocus" can solve that. While this may not be the author's intent, the question is not limited to the current application and this is the only answer that even attempts to find the actual active control regardless of where/what it is. – izzy Aug 01 '18 at 19:26