Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Know who got the focus in a Lost Focus event

Is it possible to know who got the focus in a lost focus event?

Compact Framework does not have an ActiveControl, so I don't know how to tell who got the focus.

like image 881
Vaccano Avatar asked May 24 '10 18:05

Vaccano


1 Answers

This is the solution that ended up working:

public System.Windows.Forms.Control FindFocusedControl()
{
    return FindFocusedControl(this);
}

public static System.Windows.Forms.Control FindFocusedControl(System.Windows.Forms.Control container)
{
    foreach (System.Windows.Forms.Control childControl in container.Controls)
    {
        if (childControl.Focused)
        {
            return childControl;
        }
    }

    foreach (System.Windows.Forms.Control childControl in container.Controls)
    {
        System.Windows.Forms.Control maybeFocusedControl = FindFocusedControl(childControl);
        if (maybeFocusedControl != null)
        {
            return maybeFocusedControl;
        }
    }

    return null; // Couldn't find any, darn!
}
like image 111
Vaccano Avatar answered Oct 29 '22 17:10

Vaccano