title: how to undo pictureBox drawline C# windows form using Microsoft Visual Studio
body: <p>And trying get working small paint application, that draws only lines with mouse event.</p>
  
  <p>Could someone provide me with correct working example how to create undo functionality?
  I have picturebox1 with Events pictureBox1_MouseDown, pictureBox1_MouseMove, pictureBox1_MouseUp.</p>
  
  <p>When I moving mouse and  and clicking on picture box it is drawing lines as expected.
  But how to create undo functionality for example on undo button click?</p>
  
  <pre><code>     private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
          {
  
              lx = e.X;
              ly = e.Y;
  
              Graphics g = pictureBox1.CreateGraphics();
              g.DrawLine(new Pen(new SolidBrush(paintcolor), 3), new Point(x, y), new Point(lx, ly));
  
  
              this.Invalidate();
  
          }
  
  
  
  
     private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
          {
              x = e.X;
              y = e.Y;
  
          }
  
  
  
  
    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
          {
  
          }
  </code></pre>
  
  <p>I create additionally</p>
  
  <pre><code>class Shape
  {
  
      public string _font { get; set; }
      public string g { get; set; }
      public int x { get; set; }
      public int y { get; set; }
      public int lx { get; set; }
      public int ly { get; set; }
      public string paintcolor { get; set; }
      public string s { get; set; }
  
  
  }
  </code></pre>
  
  <p>then added   under <strong>public partial class Form1 : Form</strong></p>
  
  <pre><code>private Stack&lt;Shape&gt; _shapes = new Stack&lt;Shape&gt;();
  </code></pre>
  
  <p>than add to <strong>pictureBox1_MouseUp</strong></p>
  
  <pre><code>newShape._font = _font.ToString();
              newShape.g = g.ToString();
              newShape.x = x;
              newShape.y = y;
              newShape.lx = lx;
              newShape.ly = ly;
              newShape.paintcolor = paintcolor.ToString();
  
  
  
  
                  _shapes.Push(newShape);
  
  
              this.Invalidate();
  </code></pre>
  
  <p>and for Undo button <strong>btnUndo</strong></p>
  
  <pre><code>  private void btnUndo_Click(object sender, EventArgs e)
          {
  
  
              if (_shapes.Count &gt; 0)
              {
  
                  this.pictureBox1.Invalidate();
                  _shapes.Pop();
  
  
              }
              else
              {
                  MessageBox.Show("Nothing to remove.", "No Shapes",
                      MessageBoxButtons.OK, MessageBoxIcon.Warning);
                  return;
              }
  
          }
  </code></pre>
  
  <p>and put break-point on  <strong>_shapes.Push(newShape);</strong></p>
  
  <p>that _shapes are filed with values</p>
  
  <p>and when I push button <strong>btnUndo</strong></p>
  
  <p>I see that _shapes.Pop(); removing values LIFO, but of course my lines did not disappears.
  So I'm confused because I have feeling that I need create lines on pictureBox1 not from mouse movements, but from Stack.</p>