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

66,995

Solution 1

Form.ActiveControl may be what you want.

Solution 2

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

Solution 3

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

me.ActiveControl

Or

Form.ActiveControl

Solution 4

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

Solution 5

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
Share:
66,995
Jeff
Author by

Jeff

Updated on July 09, 2022

Comments

  • Jeff
    Jeff almost 2 years

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

  • PsychoData
    PsychoData about 10 years
    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
  • miroxlav
    miroxlav over 9 years
    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.
  • izzy
    izzy almost 6 years
    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.
  • R.Alonso
    R.Alonso almost 3 years
    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