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;
}
Well... that's how it works. Continue handling the button's MouseLeave
event 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();
}
}
Put the button onto a Panel
that's sized and positioned to exactly contain the button. Then hook MouseEnter
and MouseLeave
on the panel. Show/hide the button; leave the panel always visible so it can get the mouse events.
As the terse comment suggests, invisible objects are not recognized by the mouse, because they "aren't there".
That's how it works; invisible controls do not respond to mouse events.
How about reconsidering your design? An invisible control that only appears when the mouse scrolls over it just screams "hard to use". I could understand a few child controls appearing when hovering over a parent container or control, but not a lone button, invisible until found purely by luck. You could always just wrap the button in another container and handle the container's mouse events.