Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Drawing on MouseEnter, Invalidate on MouseLeave

$
0
0
So, I'm having a problem. I have a PictureBox and a Label next to each other without a pixel space between them. Now, I call the following code for both the PictureBox and the Label.
p.MouseEnter += (s, ex) => { hover(panel, p, l); };
p.MouseLeave += (s, ex) => { panel.Invalidate(); p.Invalidate(); l.Invalidate(); };

l.MouseEnter += (s, ex) => { hover(panel, p, l); };
l.MouseLeave += (s, ex) => { panel.Invalidate(); p.Invalidate(); l.Invalidate(); };

void hover(Panel panel, PictureBox p, Label l)
        {
            int width = panel.Width - 1;
            int height = panel.Height - 1;
            Pen myPen = new Pen(Color.Red);
            Graphics PanelGraphics = panel.CreateGraphics();
            Graphics PictureGraphics = p.CreateGraphics();
            Graphics LabelGraphics = l.CreateGraphics();

            PanelGraphics.DrawLine(myPen, 0, 0, width, 0);
            PanelGraphics.DrawLine(myPen, 0, 0, 0, height);
            PanelGraphics.DrawLine(myPen, width, 0, width, height);
            PanelGraphics.DrawLine(myPen, 0, height, width, height);

            PictureGraphics.DrawLine(myPen, 0, 0, 0, height);
            PictureGraphics.DrawLine(myPen, width, 0, width, height);
            PictureGraphics.DrawLine(myPen, 0, 0, width, 0);

            LabelGraphics.DrawLine(myPen, 0, 0, 0, height);
            LabelGraphics.DrawLine(myPen, width, 0, width, height);

            myPen.Dispose();
            PanelGraphics.Dispose();
            PictureGraphics.Dispose();
            LabelGraphics.Dispose();
        }

So if I enter the PictureBox from the Form and leave it to the Form my code works as intended, drawing the lines on enter, removing them on leaving.

When I go from the PictureBox to the Label or vice versa, the lines got removed but not drawn again. So I guess when i leave the PictureBox and enter the Label the lines get drawn but immediately removed.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>