title: How to keep drawn shapes after the form is refreshed?
body: <p>I am trying to create a small paint application in Visual Studio 2015. My project falls into the category of Windows Form Applications. I have the following problem: </p>
  
  <pre><code> private void Form1_MouseMove(object sender, MouseEventArgs e)
      {
          if (a == 1)
          {
              if (r == 1 || el == 1)
              {
                  int x = Math.Min(inX, e.X);
                  int y = Math.Min(inY, e.Y);
                  int width = Math.Max(inX, e.X) - Math.Min(inX, e.X);
                  int height = Math.Max(inY, e.Y) - Math.Min(inY, e.Y);
                  rect = new Rectangle(x, y, width, height);
                  Refresh();
              }
              else if (l == 1)
              {
                  ep = e.Location;
                  Refresh();
              }
              else
              {
                  ep = e.Location;
                  g = this.CreateGraphics();
                  g.DrawLine(p, sp, ep);
                  sp = ep;
              }
          }
      }
  </code></pre>
  
  <p>This part of  my codes creates a Rectangular (<strong>2nd</strong> if), a line segment(<strong>3rd</strong> if) and just a line. It works pretty much the same as MS Paint; the rectangular or the line segment isn not completed until the user releases the left mouse click (Mouse up). But when a rectangular is finally made when I try again to create another one, the form refreshes ( <strong>Refresh();</strong> ) and I lose all the previous drawn rectangulars or lines. I tried replacing <strong>Refresh();</strong> with <strong>Invalidate(rect);</strong> and <strong>Update();</strong>, but I do not get the result I want.</p>
  
  <p>Instead, I get this: </p>
  
  <p><a href="https://i.stack.imgur.com/9stsG.png" rel="noreferrer"><img src="https://i.stack.imgur.com/9stsG.png" alt="enter image description here"></a></p>