MouseEnter and MouseLeave events from a Panel and its child controls

后端 未结 9 832
我在风中等你
我在风中等你 2020-12-10 13:14

I have a Panel that contains child controls.

If I handle the Panel\'s MouseEnter and MouseLeave events, and its c

9条回答
  •  北荒
    北荒 (楼主)
    2020-12-10 13:43

    The solution is to track the number of enters/leaves. In you overall control add a counter:

    private int mouseEnterCount = 0;
    

    In the MouseEnter handler do this:

    if (++mouseEnterCount == 1)
    {
       // do whatever needs to be done when it first enters the control.
    }
    

    In the MouseLeave handler do this:

    if (--mouseEnterCount == 0)
    {
       // do whatever needs to be done when it finally leaves the control.
    }
    

    and do the above MouseEnter and MouseLeave event handlers for ALL the child controls as well as the containing object.

提交回复
热议问题