Click here to Skip to main content
15,888,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
          //  Boolean log = false();
            this.Invalidate();
            this.Refresh();
            theta = theta - 1;
            if (theta <= 0)
            {
                theta = 180;
            }
            if (theta >= 90)
            {

                x0 = 212;
            }
           
            int x = Convert.ToInt32(x0 - r * Math.Cos(theta * Math.PI / 180));
            int y = Convert.ToInt32(y0 - r * Math.Sin(theta * Math.PI / 180));
            
            p.EndCap = System.Drawing.Drawing2D.LineCap.DiamondAnchor;
            panel1.CreateGraphics().DrawLine(p, x0, y0, x, y);
            ab=ab+2;
           
            
            
        }
Posted

1 solution

1) Yes, it will.
2) Don't do it like that.
3) Really don't do it like that or flickering will be the least of your problems...

The important one first: If you create a graphics context, you are responsible for Disposing of it. If you don't, then a very valuable system resource is "Tied up" and your system will run out of handles - and the ability to draw anything as a result - long before the Garbage Collector is fired up to remove them for you.
So each time you do this:
C#
panel1.CreateGraphics().DrawLine(p, x0, y0, x, y);

you are damaging your system slightly.
Do this, and it's better:
C#
using (Graphics g = panel1.CreateGraphics())
   {
   g.DrawLine(p, x0, y0, x, y);
   }
And that problem is averted.

The other two are related: you invalidate the form - which forces a repaint on the panel - and then draw onto the panel. Because this all involves clearing the panel's current content, you get flicker.

Don;t do it like that: instead, handle the Panel.Paint event, and draw your arrow there. The Invalidate will force it to redraw, so you won't lose the arrow if you minimise and restore your app - the restore will automatically Invalidate the form, so the panel will be redrawn. And in the Paint event, you are handed the Graphics context to use, so you don't need to create or dispose anything!

It'll still flicker though! But that's easy to fix: set the Panel.DoubleBuffered property to true, and the flicker should stop.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900