Question

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?

Was it helpful?

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.

OTHER TIPS

It's been a little while, but I'm fairly sure that if a control has .Enabled set to false, it won't respond to any event handlers. What benefit do you get from the panel being disabled? You should enable it and try then. As Poomrokc The 3years said, make sure the method is tied to the control as the event handler. You don't need to add it programmatically though. You can set it manually in the designer (not recommended) or select the panel and in the Properties panel, go to the event handlers and assign it that way.

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.X >= pnlOne.Location.X && e.X <= pnlOne.Location.X + pnlOne.Size.Width && pnlOne.Visible == false && e.Y >= pnlOne.Location.Y && e.Y <= pnlOne.Location.Y + pnlOne.Size.Height)
    {
        pnlOne.Visible = true;
        pnlOne.Enabled = true;
    }
}

private void pnlOne_MouseLeave(object sender, EventArgs e)
{
    pnlOne.Visible = false ;
    pnlOne.Enabled = false ;
}

The enable property is set to false!

Whatever object the mouse event is being used must have the enable property set TRUE!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top