What is the preferred way to find focused control in WinForms app?

.NetWinforms

.Net Problem Overview


What is the preferred/easiest way to find the control that is currently receiving user (keyboard) input in WinForms?

So far I have come up with the following:

public static Control FindFocusedControl(Control control)
{
    var container = control as ContainerControl;
    return (null != container
        ? FindFocusedControl(container.ActiveControl)
        : control);
}

From a form, this can be called simply as (in .NET 3.5+ this could even be defined as an extension method on the form) -

var focused = FindFocusedControl(this);

Is this appropriate?

Is there a built-in method that I should be using instead?

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 (because ActiveControl seems to only be returning immediate active child in the control tree, while I'm looking for the leaf control).

.Net Solutions


Solution 1 - .Net

If you have other calls to the Windows API already, there's no harm in using Peters solution. But I understand your worries about it and would tend to a similar solution as yours, using only the Framework functionalities. After all, the performance difference (if there is one) shouldn't be significant.

I would take a non recursive approach:

public static Control FindFocusedControl(Control control)
{
    var container = control as IContainerControl;
    while (container != null)
    {
        control = container.ActiveControl;
        container = control as IContainerControl;
    }
    return control;
}

Solution 2 - .Net

After searching the Internet, I found the following on George Shepherd's Windows Forms FAQ

> The .Net framework libraries does not provide you an API to query for > the focused Control. You have to > invoke a windows API to do so: > > [C#]

public class MyForm : Form
{
          [DllImport("user32.dll", CharSet=CharSet.Auto, CallingConvention=CallingConvention.Winapi)]
          internal static extern IntPtr GetFocus();

          private Control GetFocusedControl()
          {
               Control focusedControl = null;
               // To get hold of the focused control:
               IntPtr focusedHandle = GetFocus();
               if(focusedHandle != IntPtr.Zero)
                    // Note that if the focused Control is not a .Net control, then this will return null.
                    focusedControl = Control.FromHandle(focusedHandle);
               return focusedControl;
          }
} 

Solution 3 - .Net

ActiveControl on a Form or Container will return that entity's active control no matter how deeply it might be nested inside other containers.

In your example if the TextBox has Focus : then : for Form, TableLayoutPanel, and FlowLayoutPanel : the 'ActiveControl property of all of them will be the TextBox !

Some, but not all, "genuine" ContainerControl types ... like Form and UserControl ... expose Key Events (in the case of Form : only if Form.KeyPreview == true can they be used) .

Other controls which, by design, contain other controls like TableLayOutPanel, GroupBox, Panel, FlowLayoutPanel, etc. are not type ContainerControl, and they do not expose KeyEvents.

Any attempt to cast instances of objects like TextBox, FlowLayoutPanel, TableLayoutPanel directly to ContainerControl will not compile : they are not type ContainerControl.

The code in the accepted answer, and in the next answer that corrects the first answer's spelling errors, will compile/accept instances of the above as parameters because you are "downcasting" them to type 'Control by making the parameter type 'Control

But in each case the cast to ControlContainer will return null, and the passed in instance will be returned (downcasted) : essentially a no-op.

And, yes, the modified answer code will work if you pass it a "genuine" ControlContainer, like a Form instance, which is in the parent inheritance path of the ActiveControl, but you are still just wasting time duplicating the function of 'ActiveControl.

So what are "genuine" ContainerControls : check them out : MS docs for ContainerControl

Only the answer by Peter really answers the explicit question, but that answer carries the price of using interop, and 'ActiveControl will give you what you need.

Also note that every Control (container or non-container) has a Controls Collection that is never null, and that a lot of (I've never tried all of them : why would I ?) the basic WinForms control let you do "crazy stuff" like adding Controls to the ControlCollection of 'simple' controls like Button without an error.

Now if the real intent of your question was to ask how you find the outermost ContainerControl ... that is not on the Form itself ... of a regular non-container Control nested some arbitrary levels deep ... you can use some of the ideas in the answer : but the code can be greatly simplified.

Regular Controls, ContainerControls, UserControls, etc. (but not Form !) all have a 'Container property you can access to get their immediate container, but making sure you have the 'final Container in their inhertance path that's not a Form requires some code to "walk-up" the inheritance tree, which is demonstrated here.

You may also wish to check out the 'HasChildren property of 'Control which is usually useful in dealing with issues of Focus, ActiveControl, and Select in WinForms. Reviewing the difference between Select and Focus can be valuable here, and SO has some good resources on that.

Hope this helps.

Solution 4 - .Net

Hinek's solution works well for me, except it is ContainerControl, not ControlContainer. (Just in case you were scratching your head about that red squiggly line.)

    public static Control FindFocusedControl(Control control)
    {
        ContainerControl container = control as ContainerControl;
        while (container != null)
        {
            control = container.ActiveControl;
            container = control as ContainerControl;
        }
        return control;
    }

Solution 5 - .Net

If you follow ActiveControl out recursively it doesn't take you to the leaf control that has focus?

Solution 6 - .Net

ActiveControl doesn't always work out, like with SplitContainer, ActiveControl.Focused is false.

So for a more fool proof method could do something like this:

private IEnumerable<Control> _get_all_controls(Control c)
{
	return c.Controls.Cast<Control>().SelectMany(item =>
		_get_all_controls(item)).Concat(c.Controls.Cast<Control>()).Where(control =>
		control.Name != string.Empty);
}

var _controls = _get_all_controls(this);
foreach (Control control in _controls) 
	if (control.Focused)
	{
    	Console.WriteLine(control.Name);
    	break;
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMilan GardianView Question on Stackoverflow
Solution 1 - .NetHinekView Answer on Stackoverflow
Solution 2 - .NetXn0vv3rView Answer on Stackoverflow
Solution 3 - .NetBillWView Answer on Stackoverflow
Solution 4 - .NetNate CookView Answer on Stackoverflow
Solution 5 - .NetsliderhouserulesView Answer on Stackoverflow
Solution 6 - .Netcolin lamarreView Answer on Stackoverflow