4

I think this is a stupid question, but I don't understand what is happening here.

I use 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: The picturebox changes color to black if mouse is over picturebox, but the visibility doesn't change. Why?

Arin
  • 1,373
  • 2
  • 10
  • 23
RedoColor
  • 137
  • 1
  • 3
  • 14
  • 2
    I guess that the MouseLeave event is called when you set the picturebox visibility to off. – Graffito Aug 03 '15 at 14:36
  • i need to click and press click to disappear. after i don't press mouse click the picturebox reappear but i don't understand why.... why i need to press click when i use MouseHover Event ???? – RedoColor Aug 03 '15 at 14:42
  • 1
    It is just as Graffito says: Once the PB is no longer __visible__ the mouse is no longer over it. Therefore MouseLeave is triggered right away. If you use MouseEnter you will get a flicker, but MouseHover is not called without moving the mouse.. BTW: I can neither think of a simple solution not of any reason why one would code such a thing in the first place.. – TaW Aug 03 '15 at 14:45
  • 1
    @TaW, simple solution is to use same shaped control underneath to catch its `MouseLeave` to make picturebox visible again. And it so obvious, cba to post as an answer. – Sinatr Aug 03 '15 at 14:48
  • Yes, or do some timer tricks etc. But anything involving another object is not really simple in my book.. ;-) – TaW Aug 03 '15 at 14:49

2 Answers2

1

You can use MouseEnter event instead of MouseHover and bool field isHover that you can use in attempt to reduce flickering :

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;
    }

...

}
Fabjan
  • 13,506
  • 4
  • 25
  • 52
  • This is neither the problem not the solution. You ought to test code you post..! – TaW Aug 03 '15 at 14:46
  • 1
    @TaW just tested it and it works. Control disappears and with Hover it doesnt – Fabjan Aug 03 '15 at 14:49
  • It doesn't really works. It only makes you think it does. To prove it, add `Text += "+";` in `MouseEnter` and `Text += "-";` to `MouseLeave` and you will see how it flickers a lot, form tittle will get a lot of those `"+-+-+-..."` on just one mouse moving inside. But you don't see it, because it become invisible faster than redraw occurs for some reasons (which I don't know). – Sinatr Aug 03 '15 at 14:54
1

I think your problem is as soon as you hover the picture it really disappears (that's why you see the back color turn to black, the event is firing). However the picture disappears which lead to situation where your mouse is not on the picture anymore, therefore Mouse_Leave event firing.

Felix Av
  • 1,254
  • 1
  • 14
  • 22