Is there a way to catch when ContainsFocus changes?

I need to detect when ContainsFocus

changes in Control

(specifically in the window form). Overriding OnGotFocus

is not the answer. When I bring the shape to front, ContainsFocus

it is true and Focused

false. So, is there an OnGotFocus

equivalent for ContainsFocus

? Or in any other way?

0


source to share


3 answers


Note. GotFocus events for child controls are fired when you have a child control. Otherwise, the OnGotFocus of the form is called.

If I understood the question correctly, then this should work:



    bool lastNotificationWasGotFocus = false;

    protected override void OnControlAdded(ControlEventArgs e)
    {
        SubscribeEvents(e.Control);
        base.OnControlAdded(e);
    }

    protected override void OnControlRemoved(ControlEventArgs e)
    {
        UnsubscribeEvents(e.Control);
        base.OnControlRemoved(e);
    }

    private void SubscribeEvents(Control control)
    {
        control.GotFocus += new EventHandler(control_GotFocus);
        control.LostFocus += new EventHandler(control_LostFocus);
        control.ControlAdded += new ControlEventHandler(control_ControlAdded);
        control.ControlRemoved += new ControlEventHandler(control_ControlRemoved);

        foreach (Control innerControl in control.Controls)
        {
            SubscribeEvents(innerControl);
        }
    }

    private void UnsubscribeEvents(Control control)
    {
        control.GotFocus -= new EventHandler(control_GotFocus);
        control.LostFocus -= new EventHandler(control_LostFocus);
        control.ControlAdded -= new ControlEventHandler(control_ControlAdded);
        control.ControlRemoved -= new ControlEventHandler(control_ControlRemoved);

        foreach (Control innerControl in control.Controls)
        {
            UnsubscribeEvents(innerControl);
        }
    }

    private void control_ControlAdded(object sender, ControlEventArgs e)
    {
        SubscribeEvents(e.Control);
    }

    private void control_ControlRemoved(object sender, ControlEventArgs e)
    {
        UnsubscribeEvents(e.Control);
    }

    protected override void OnGotFocus(EventArgs e)
    {
        CheckContainsFocus();
        base.OnGotFocus(e);
    }

    protected override void OnLostFocus(EventArgs e)
    {
        CheckLostFocus();
        base.OnLostFocus(e);
    }

    private void control_GotFocus(object sender, EventArgs e)
    {
        CheckContainsFocus();
    }

    private void control_LostFocus(object sender, EventArgs e)
    {
        CheckLostFocus();
    }

    private void CheckContainsFocus()
    {
        if (lastNotificationWasGotFocus == false)
        {
            lastNotificationWasGotFocus = true;
            OnContainsFocus();
        }
    }

    private void CheckLostFocus()
    {
        if (ContainsFocus == false)
        {
            lastNotificationWasGotFocus = false;
            OnLostFocus();
        }
    }

    private void OnContainsFocus()
    {
        Console.WriteLine("I have the power of focus!");
    }

    private void OnLostFocus()
    {
        Console.WriteLine("I lost my power...");
    }

      

+2


source


One way to solve this problem is to use a timer. It's definitely brute force, but it gets the job done:

private Timer m_checkContainsFocusTimer = new Timer();
private bool m_containsFocus = true;

m_checkContainsFocusTimer.Interval = 1000; // every second is good enough
m_checkContainsFocusTimer.Tick += new EventHandler(CheckContainsFocusTimer_Tick);
m_checkContainsFocusTimer.Start();

private void CheckContainsFocusTimer_Tick(object sender, EventArgs e)
{
    if (!m_containsFocus && ContainsFocus)
        OnAppGotFocus();

    m_containsFocus = ContainsFocus;
}

      



But is there an easier way?

0


source


Working with the GotFocus and LostFocus events should do this.

One more note ... The SDK says about this ContainsFocus property:

You can use this property to determine whether the control or any of the controls it contains has input focus. To determine if a control has focus, regardless of whether any of its child controls have focus, use the Focused property.

EDIT:

When handling the GotFocus event, you will still have to check the Focused / ContainsFocus property depending on how your control hierarchy is configured.

ContainsFocus will be true if the control or any of its children have focus. Focus will only be right if the particular control itself has focus, regardless of its children.

0


source







All Articles