Alex Rivera | Logout

Know who got the focus in a Lost Focus event

Asked 2010-05-24T18:36:03.817
9

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.

Edit
Report

1 Answer

6

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!
}
answered 2010-05-25T14:58:13.610

Your Answer