Make PictureBox visible / invisible with MouseHover

I think this is a stupid question, but I do not understand what is happening here.

I am using this code:

private void pictureBox1_MouseHover(object sender, EventArgs e) { pictureBox1.Visible = false; pictureBox1.BackColor = Color.Black; } private void pictureBox1_MouseLeave(object sender, EventArgs e) { pictureBox1.Visible = true; } 

The problem is this: the picture changes color to black if the mouse is above the frame, but the visibility does not change. Why?

+4
source share
2 answers

You can use the MouseEnter event instead of the MouseHover and bool field isHover , which you can use to reduce flicker:

 public partial class Form1: Form { bool isHover = false; private void pictureBox1_MouseEnter(object sender, EventArgs e) { if(isHover) return; // with MouseHover this control visibility appears to be locked with MouseEnter it is not pictureBox2.Visible = false; pictureBox2.BackColor = Color.Black; } private void pictureBox1_MouseLeave(object sender, EventArgs e) { if(!isHover) return; isHover = false; pictureBox2.Visible = true; } ... } 
+1
source

I think that your problem, as soon as you hover over the image, it really disappears (why do you see that the back color turns to black, the event shoots). However, the image disappears, which leads to a situation where your mouse is no longer in the picture, so the Mouse_Leave event fires.

+1
source

All Articles