-1

First I'm drawing one line

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{ 
   e.Graphics.DrawLine(System.Drawing.Pens.White, dx1, dy1, dx2, dy2);            
}

then I'm in other function changing variables dx1, dy1, dx2, dy2 and calling pictureBox1.Refresh();

After that I have a new line painted, but old one disappeared. How can I add a new one without disappearing of old one?

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • 3
    Don't forget about the "old" ones, use Graphics.DrawLines() instead. Or draw into a bitmap. – Hans Passant Oct 26 '15 at 15:31
  • 1
    The Paint event must draw all things you want to draw onto the control. Store all data in Lists ! For the difference between drawing __onto__ a control surface and __into__ a Bitmap the control displays, see [here](http://stackoverflow.com/questions/27337825/picturebox-paintevent-with-other-method/27341797?s=2|0.0381#27341797)! For a longer discussion [see here](http://stackoverflow.com/questions/28714411/update-a-drawing-without-deleting-the-previous-one/28716887?s=9|0.1773#28716887) – TaW Oct 26 '15 at 15:38
  • For additional inspiration, see also [How to retain previous graphics in paint event?](http://stackoverflow.com/q/27488584) and [Why does text drawn on a panel disappear?](http://stackoverflow.com/q/946150), among the many other questions related to trying to draw new things on a form without losing the old things. – Peter Duniho Oct 26 '15 at 16:32

1 Answers1

-2

Try starting with an array of points, then handle the event by adding new points to the array, then draw the lines:

Point[] points = new Point[1];

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    int ptCount = points.Count();
    Array.Resize(ref points, ptCount + newPointAmt);


    // Add new points here.

    g.Clear(this.BackColor);
    g.DrawLines(new Pen(Color.White), points);
}
Tyler Pantuso
  • 835
  • 8
  • 16