Search code examples
c#buttonvisiblemousehover

MouseHover and MouseLeave don't work when the control is invisible


For some reason MouseHover and MouseLeave functions behave really strange. All I need to do is, when the cursor is over the "button", I want to make the button visible and when the cursor leaves the button, I want to make it invisible. Whatever I try I couldn't make it work. It seems like Mouse events not working when the control object is invisible.

private void button1_MouseHover(object sender, EventArgs e)
{
   button1.Visible = true;
}

private void button1_MouseLeave(object sender, EventArgs e)
{
    button1.Visible = false;
}

Solution

  • That’s how it works. One option is to continue handling the button's MouseLeave event the same way, and handle MouseMove for its parent (I assume the form):

    private void Form_MouseMove(object sender, MouseEventArgs e) {
        if (button1.Bounds.Contains(e.Location) && !button1.Visible) {
            button1.Show();
        }
    }