Search code examples
c#winformspanel

Mouse Move event not working for me on Panel


I m working on project to play video. I used play/pause/stop buttons in panel and panel is disabled and not visible initially.

I want to enable it by mouse move event, but when I move cursor in panel contained area, it does not show me panel control, here is code..

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.X >= top_left.X && e.X <= top_right.X && e.Y >= top_left.Y && e.Y <= bottom_left.Y)
         SetEnabled(true);
    else SetSenabled(false);

    void SetEnabled(bool enabled) => (panel1.Visible, panel1.Enabled) = (enabled, enabled);
}

What can I do now?


Solution

  • An invisible panel won't throw any events, so the only way to really do this is with a timer and check whether or not the mouse is inside the control's area or not:

    private Timer timer = new Timer();
    
    public Form1() {
      InitializeComponent();
      timer.Tick += timer_Tick;
      timer.Start();
    }
    
    void timer_Tick(object sender, EventArgs e) {
      Rectangle r = pnlOne.RectangleToScreen(pnlOne.ClientRectangle);
      if (r.Contains(MousePosition)) {
        if (!pnlOne.Visible)
          pnlOne.Visible = true;
      } else {
        if (pnlOne.Visible)
          pnlOne.Visible = false;
      }
    }
    

    No need to change the panel's Enabled property (just leave it Enabled=true;) since you seem to only care to show the panel when the mouse is in the control's area.