0

Hi i am trying to make a panel that will show some text when it hovered over a picture and i want it to follow the cursor so i

System.Windows.Forms.Panel pan = new System.Windows.Forms.Panel();
    public Form1()
    {
        InitializeComponent();
        Product p = new Product();
        p.SetValues();
        this.pictureBox1.Image = (Image)Properties.Resources.ResourceManager.GetObject("pictureName");

    }

    private void pictureBox1_MouseEnter(object sender, EventArgs e)
    {
        pan.Height = 200;
        pan.Width = 100;
        pan.BackColor = Color.Blue;
        this.Controls.Add(pan);
        pan.BringToFront();
        //pan.Location = PointToClient(Cursor.Position);
    }

    private void pictureBox1_MouseLeave(object sender, EventArgs e)
    {
        Controls.Remove(pan);
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        pan.Location = PointToClient(Cursor.Position);  
    }

I tried adding this.doublebuffered = true; but it just make it look like there are after image of the panel when i move my mouse

when i hover over my picture it shows the panel but it flickers like crazy is this normal or is there a fix for this or this is my computers problem

Annonymous177
  • 563
  • 2
  • 10
  • 21
  • I assume that the panels is redrawn everytime you move your mouse. Maybe only use the `MouseHover` event (no `MouseMove`) and set the [MousHoverTime](https://msdn.microsoft.com/en-us/library/system.windows.forms.systeminformation.mousehovertime(v=vs.110).aspx) to see if that works propertly. – Chrono Dec 24 '15 at 07:41
  • Possible duplicate of [how to stop flickering C# winforms](http://stackoverflow.com/questions/8046560/how-to-stop-flickering-c-sharp-winforms) – laminatefish Dec 24 '15 at 07:54
  • oh sorry i change the action from hover to enter i was just reusing the code – Annonymous177 Dec 24 '15 at 07:57
  • I thought so. Do edit the question! – TaW Dec 24 '15 at 07:58

1 Answers1

3

Adding this.DoubleDuffered = true; to the Form does only affect the Form, not the Panel.

So use a doubleBuffered Panel subclass:

class DrawPanel : Panel
{
    public DrawPanel ()
    {
      this.DoubleBuffered = true;
    }
}

However moving large things around will take its toll. Btw, the PictureBox class is already doubleBuffered. Also it seems more logical to add the Panel to the PictureBox, not the Form: pictureBox1.Controls.Add(pan); And adding a pictureBox1.Refresh(); to the MouseMove.

Update: As you don't draw on the Panel and also need to it to overlap the PictureBox the above ideas do not really apply; using the subclass is not necessary, although it may come handy at some point. And yes, the Panel needs to be added to the Form's Controls collection!

This code works just fine here:

public Form1()
{
    InitializeComponent();

    // your other init code here

    Controls.Add(pan);  // add only once
    pan.BringToFront();
    pan.Hide();         // and hide or show
    this.DoubleDuffered = true  // !!
}

private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
    pan.Hide();         // hide or show
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    pan.Location = pictureBox1.PointToClient(Cursor.Position);
    pictureBox1.Refresh();  // !!
    Refresh();              // !!
}

private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
    pan.Height = 200;
    pan.Width = 100;
    pan.BackColor = Color.Blue;
    pan.Location = pictureBox1.PointToClient(Cursor.Position);
    pan.Show();        // and hide or show
}

Looks like you were missing the right combination of doublebuffering the Form and refreshing both, the Form and the PictureBox.

TaW
  • 53,122
  • 8
  • 69
  • 111
  • Nope the panel still flickers when moving the mouse even though it happen less – Annonymous177 Dec 24 '15 at 07:49
  • Actually I tried your code as it is and see __no__ flicker. How large is the image? – TaW Dec 24 '15 at 07:52
  • width is 209 height is 319 – Annonymous177 Dec 24 '15 at 07:53
  • still have flickering even when the mouse does not move but it a huge improvement as it only flicker a few times only – Annonymous177 Dec 24 '15 at 08:02
  • adding the panel to the picturebox1 does not even show the panel as it is trap within the picturebox1 display area – Annonymous177 Dec 24 '15 at 08:06
  • apparently using `e.location;` makes it so that it will get the position of the cursor relative to the image the put it at the form so when i hover my cursor at the pictureBox1 position at (0,0) then the panel will appear at (0,0) so in a way i just need to add position of the picturebox to the `e.location` code – Annonymous177 Dec 24 '15 at 08:17
  • That works if and only if the Panel is nested in the PictureBox. Do you need to let the Panel to overlap the PictureBox? It is larger large for a 209 x319 picturebox! – TaW Dec 24 '15 at 08:18
  • yeah i need the panel to be outside of the picture box – Annonymous177 Dec 24 '15 at 08:20
  • Ah, now it makes sense! I habe modified the answer.. Looks like you were missing the right combination of doublebuffering the form and refreshing both, the form and the picturebox.. – TaW Dec 24 '15 at 08:26
  • It work perfectly now as i change the line of `pictureBox1.PointToClient(Cursor.Position);` to `this.PointToClient(Cursor.Position)` and now it wokes the way i wanted it to be Thanks alot for helping me – Annonymous177 Dec 24 '15 at 08:32