MouseEnter and MouseLeave events from a Panel and its child controls

后端 未结 9 806
我在风中等你
我在风中等你 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:22

    If you dont mind creating a usercontrol(derived from the panel or other parent container you wish), Override your parent's OnMouseLeave method to look like the following..

    protected override void OnMouseLeave(EventArgs e)
    {
    
        if(this.ClientRectangle.Contains(this.PointToClient(Control.MousePosition)))
             return;
        else
        {
            base.OnMouseLeave(e);
        }
    }
    

    Then, the event raising will be in the required order.

    0 讨论(0)
  • 2020-12-10 13:25

    This may not be the most elegant solution, but you could set a property in the parent control panel (subclass panel) that is a bool value like "bool selected". Then when the MouseEnter for the panel fires set it to true...then stop the mouseleave logic from firing unless it is set to false.

    example

    bool selected;
    
    MouseEnter(..,..)
    {
     if (!selected)
       selected = true;
     else
       selected = false;
    
      if (selected)
       /.. Logic Here ../
    }
    
    MouseLeave()
    {
      if (selected)
       return;
    
    /.. Logic Here ../
    }
    

    In reality I would just have the MouseLeave event of the child set the parameter.

    Example:
    
    Parent:
      bool doLeave;
    
      MouseLeave(..,..)
      {
        if (doLeave)
        {
         /.. Logic ../
         doLeave = false;
      }
    
    Child:
     MouseLeave(..., ...)
      {
        DerivedPanel parent = this.Parent as DerivedPanel;
        if (parent != null)
          parent.doLeave = true;
      }
    

    Neither are elegant but it will work.

    0 讨论(0)
  • 2020-12-10 13:25

    Jimmy T. is right. There will be problems if there is no (or small) space betwean Parent Control (Panel) edge and Child Control.

    This is how I solve this problem in UserControl-derived class:

        public CSStackPanelItem()
        {
            InitializeComponent();
    
            MouseEnter += new EventHandler(CSStackPanelItem_MouseEnter);
    
            foreach (Control child in Controls)
            {
                child.MouseEnter += (s, e) => CSStackPanelItem_MouseEnter(s, e);
                child.MouseLeave += (s, e) => OnMouseLeave(e);
            }
        }
    
        protected override void OnMouseLeave(EventArgs e)
        {
            if (this.ClientRectangle.Contains(this.PointToClient(Control.MousePosition)))
                return; //suppress mouse leave event handling
    
            if (m_bIsHover)
            {
                m_bIsHover = false;
                Invalidate(); //actually my mouse Enter/Leave event
            }
    
            base.OnMouseLeave(e);
        }
    
        void CSStackPanelItem_MouseEnter(object sender, EventArgs e)
        {
            m_bIsHover = true;
            Invalidate(); //actually my mouse Enter/Leave event
        }
    
    0 讨论(0)
  • 2020-12-10 13:30

    Matthew's answer will not work always. Especially if the child control is set to the edge of its container and the mouse moves off the controls in that direction. You will never detect the MouseLeave event.

    The best approach is to create a user control container then hook all the child controls' MouseEnter and MouseLeave events so that you can properly detect when and where the mouse is at all times. THEN if it enters your container's bounds you can fire a custom MouseEnter event and when it leaves MouseLeave event.

    0 讨论(0)
  • 2020-12-10 13:32

    I believe so. A nice tool to have for verifying your WinForms application's events.

    Windows.Forms Order of Events

    http://missico.spaces.live.com/blog/cns!7178D2C79BA0A7E3!186.entry

    • Created with EventSpy written by Urs Eichmann. (ftp://missico.net/EventSpy.zip)
    • Using .NET Framework 3.5 and with Visual Basic’s Application Framework enabled.
    0 讨论(0)
  • 2020-12-10 13:35

    Check the child component..

    if (panel1.GetChildAtPoint(panel1.PointToClient(Cursor.Position)) == null)
    {
        // mouse leave panel and children
    }
    
    0 讨论(0)
提交回复
热议问题