Question

Please note I'm trying to do the following, when I hover the mouse on a button I want the panel to be visible, when the mouse leaves the button or panel, the panel should not be visible. Below you can see the code I have, but the panel it's not staying visible.

    private void FormMain()
    {
        buttonMenu.MouseEnter += new EventHandler(buttonMenu_MouseEnter); //open panel
        buttonMenu.MouseLeave += new EventHandler(buttonMenu_MouseLeave);
        panelMenu.MouseEnter += new EventHandler(panelMenu_MouseEnter);
        panelMenu.MouseLeave += new EventHandler(panelMenu_MouseLeave);
        mbB1.MouseEnter += new EventHandler(mbB1_MouseEnter);//button in panel
        mbB2.MouseEnter += new EventHandler(mbB2_MouseEnter);//button in panel

    }

    private void buttonMenu_MouseEnter(object sender, EventArgs e)
    {
        this.panelMenu.Visible = true;
    }
    private void buttonMenu_MouseLeave(object sender, EventArgs e)
    {
        this.panelMenu.Visible = false;
    }
    private void panelMenu_MouseEnter(object sender, EventArgs e)
    {
        this.panelMenu.Visible = true;
    }
    private void panelMenu_MouseLeave(object sender, EventArgs e)
    {
        this.panelMenu.Visible = false;
    }
    private void mbB1_MouseEnter(object sender, EventArgs e)
    {
        this.panelMenu.Visible = true;
    }
    private void mbB2_MouseEnter(object sender, EventArgs e)
    {
        this.panelMenu.Visible = true;
    }
Was it helpful?

Solution

One solution that came to my mind is using a short timer. You may further optimize it to cut down on LoC, but it works. You may also lower the timer's delay, but 100ms is what I think would be a safe one.

On a side note, I don't think this is a good design. If you want this kind of behaviour, you should either use a ContextMenuStrip or a Click event on the button, and a hide event on panelMenu.MouseLeave. Still, if it's what you really need, this is how I solved it:

public partial class Form1 : Form
{
    private bool mouseInPanel;
    private Timer hideTimer;

    public Form1()
    {
        InitializeComponent();

        buttonMenu.MouseEnter += new EventHandler(button_MouseEnter);
        buttonMenu.MouseLeave += new EventHandler(button_MouseLeave);
        mbB1.MouseEnter += panelButton_MouseEnter;
        mbB2.MouseEnter += panelButton_MouseEnter;
        panelMenu.MouseEnter += new EventHandler(panelMenu_MouseEnter);
        panelMenu.MouseLeave += new EventHandler(panelMenu_MouseLeave);

        hideTimer = new Timer {Interval = 100};
        hideTimer.Tick += hidePanel;
    }

    private void button_MouseEnter(object sender, EventArgs e)
    {
        this.panelMenu.Visible = true;
    }

    private void button_MouseLeave(object sender, EventArgs e)
    {
        hideTimer.Start();
    }

    private void panelMenu_MouseEnter(object sender, EventArgs e)
    {
        mouseInPanel = true;
        this.panelMenu.Visible = true;
    }
    private void panelMenu_MouseLeave(object sender, EventArgs e)
    {
        mouseInPanel = false;
        hideTimer.Start();
    }

    private void panelButton_MouseEnter(object sender, EventArgs e)
    {
        mouseInPanel = true;
        this.panelMenu.Visible = true;
    }

    private void hidePanel(object sender, EventArgs e)
    {
        hideTimer.Stop();
        if (!mouseInPanel) this.panelMenu.Visible = false;
    }
}

What I figured is your mouse actions needed a little bit of a delay, else the panel (along with your buttons mbB1 and mbB2) gets hidden before those buttons' actions could be triggered.

This is because by entering the panel buttons you leave the panel, and it disappears (along with it's capability to receive mouse actions) right before mbB1/mbB2's action can trigger.

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