0

I'm trying to reliably detect when focus leaves a group of controls. So if I tab or click between two controls in the group, that's considered not leaving, but as soon as I click on a control outside the group, it raises an event. Here's how I'm currently doing it:

public class LeaveTracker
{
    private HashSet<Control> trackedControls = new HashSet<Control>();
    public void Track(Control ctrl)
    {
        ctrl.Leave += Ctrl_Leave;
        trackedControls.Add(ctrl);
    }

    private void Ctrl_Leave(object sender, EventArgs e)
    {
        foreach (var ctrl in trackedControls)
            if (ctrl.ContainsFocus)
                return;
        if (LeftControlGroup != null)
            LeftControlGroup(this, EventArgs.Empty);
    }
    public event EventHandler LeftControlGroup;
}

Sometimes it works perfectly, but occasionally it seems to stop raising events. Is there a more reliable way to do this?

Bryce Wagner
  • 1,151
  • 7
  • 18
  • If you can place these controls on a panel, you can just use the `Leave` event of the panel instead. – 41686d6564 stands w. Palestine Nov 21 '18 at 21:25
  • Have you tried putting the sets of controls inside another custom User Control? I believe that User Control will have its own Leave event – JayV Nov 21 '18 at 21:26
  • There are other controls that are intermixed that aren't part of the group. – Bryce Wagner Nov 21 '18 at 21:35
  • It's really hard to picture how that is supposed to work. You have a screen shot of your form that shows these "groups"? – LarsTech Nov 21 '18 at 21:36
  • @AhmedAbdelhameed This question is in no way a duplicate of the mouse leaving event. This is about focus, not mouse pointer. – Bryce Wagner Nov 21 '18 at 21:37
  • @AhmedAbdelhameed I don't see MouseHover anywhere in the referenced post. I re-opened it since it's about leaving controls. I'm still confused though. – LarsTech Nov 21 '18 at 21:37
  • @LarsTech That's focused on the MouseLeave event. Like MouseEnter/MouseMove/MouseLeave are all mouse cursor position events, nothing to do with focus. – Bryce Wagner Nov 21 '18 at 21:38
  • @LarsTech From the other question: _"The goal is when the user hoovers the mouse over the user control"_. I'd say the OP's question is very clear. He's trying to trigger an event whenever a group of _known_ controls (whatever they are) do not have focus. – 41686d6564 stands w. Palestine Nov 21 '18 at 21:39
  • The forms are pretty simple, a bunch of text boxes, comboboxes, radio buttons, checkboxes, etc. I am porting an application written in MSAccess and trying to preserve the triggered behavior of when to save edited data. – Bryce Wagner Nov 21 '18 at 21:41
  • 1
    Probably too many controls to see what is really going on. Hard to see that the Leave event is not raised at the exact moment you hope. It fires a bit too early, the control's ContainsFocus property is still true. There is a simple and elegant fix, use this.BeginInvoke(). The invoked code runs right after the event, after the focus has changed. – Hans Passant Nov 21 '18 at 22:00

0 Answers0