2

I was just wondering if you could help me with this problem.

In my program I must use the paint event to draw a line which acts as a "turret". I must use a trackbar to choose the angle at which I will "fire" this turret. My problem is I'm not sure how to code it so that the "Turret" line is redrawn every time the value in the trackbar is changed. I tried using Refresh() which allowed the line to be drawn in realtime but it caused the form to flicker black and white and to redraw the line at different angles to what was chosen also.

Any help would be greatly appreciated. Thanks!

Here is a screenshot of the form, from the below code running in Visual Basic 2010

public partial class Form1 : Form
    {
            double xEnd = 0;
            double yEnd = 0;
            double xOrigin = 30;
            double yOrigin = 450;
            double xHor = 30;
            double yHor = 350;
            double xVert = 130;
            double yVert = 450;
            double lineLength = 100;
            public Form1()
            {
                xEnd = xOrigin + lineLength;
                yEnd = yOrigin;
                InitializeComponent();
            }

            private void LineDrawEvent(object sender, PaintEventArgs paint)
            {
                Graphics drawSurface = paint.Graphics;
                Pen turretLine = new Pen(Color.Blue);
                Pen graphHorizontal = new Pen(Color.Red);
                Pen graphVertical = new Pen(Color.Red);
                Pen firedLine = new Pen(Color.Blue);

                drawSurface.DrawLine(graphVertical, (int)xOrigin, (int)yOrigin, (int)xHor, (int)yHor);
                drawSurface.DrawLine(graphHorizontal, (int)xOrigin, (int)yOrigin, (int)xVert, (int)yVert);

                double angleInRadians = ConvertDegsToRads((double)trckBarAngle.Value);
                xEnd = xOrigin + lineLength * Math.Cos(angleInRadians);
                yEnd = yOrigin - lineLength * Math.Sin(angleInRadians);

                drawSurface.DrawLine(turretLine, (int)xOrigin, (int)yOrigin, (int)xEnd, (int)yEnd);
            }
     private void trckBarAngle_Scroll(object sender, EventArgs e)
            {
                lblAngle.Text = "Angle is:" + Convert.ToString((double)trckBarAngle.Value / 2);
            }
            private double ConvertDegsToRads(double degrees)
            {
                return degrees * (Math.PI / 180.0);
            }
        }
  • The the form.DoubleBuffered = true. – TaW Oct 31 '15 at 21:41
  • I changed that in the forms properties but unfortunately that didn't make a difference. What exactly should that have changed? I'm new to c# so sorry for the noob questions. – Brian Leigh Oct 31 '15 at 21:54
  • It should stop the flickering. – TaW Oct 31 '15 at 21:56
  • Ah it did too, thanks! However now none of the labels show up and all the buttons and trackbar have a black outline. – Brian Leigh Oct 31 '15 at 21:58
  • Did you create the Paintargs yourself? Don't do that. use Paint event an trigger it with Invalidate() – TaW Oct 31 '15 at 22:08
  • No I used the events tab in the forms properties to create it. What exactly would Invalidate() do in that case? – Brian Leigh Oct 31 '15 at 22:13
  • It triggers the Paint event. That's the proper way to draw graphics onto controls.. So instead of calling it directly always use Invalidate or Refresh ! This way the base events get called as well and the rest of the necessary stuff is done.. - Here one place to call Invalidate is the trckBarAngle_Scroll event. – TaW Oct 31 '15 at 22:16
  • Awesome, thanks for all the info! – Brian Leigh Oct 31 '15 at 22:21

1 Answers1

0

You can set the form to double buffered which should stop the flicker. this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint |ControlStyles.AllPaintingInWmPaint |ControlStyles.OptimizedDoubleBuffer true); this.UpdateStyles();

In your form's constructor or OnLoad method

Or by setting the DoubleBufferered property to true. I usually perform a combination of both.

I think OptimizedDoubleBuffer is the same as the other three flags combined. However without the other two, DoubleBuffered seems to have no affect.

pinkfloydx33
  • 11,863
  • 3
  • 46
  • 63
  • Yeah I changed it to true in the form properties and it did prevent the flickering however the labels disappeared and the trackbar and buttons had a black outline around them, I'm not sure why though? – Brian Leigh Oct 31 '15 at 23:23