title: c# draw rectangle in every form
body: <p>I have a project in c# with many forms. Imagine this scenario:</p>
  
  <p>Every form has a (let's say green) rectangle in it. when I call a function, i need every rectangle to change it's color (to red). I have created this function in order to draw a rectangle:</p>
  
  <pre><code>private void drawBorder(System.Drawing.Color c)
  {
      System.Drawing.Graphics g = this.CreateGraphics();
      System.Drawing.Brush br = new System.Drawing.SolidBrush(c);
      System.Drawing.Pen p = new System.Drawing.Pen(br, 4);
  
      System.Drawing.Rectangle r = new Rectangle(0, 0, 50, 50);
      g.DrawRectangle(p, r);
      g.Dispose();
      br.Dispose();
      p.Dispose();
  }
  </code></pre>
  
  <p>I have realized that I cannot call this function on load (is this true?) <strong>This just was my first question needing an answer</strong></p>
  
  <p>So, if i had just one form, I would first call this function on paint:</p>
  
  <pre><code>private void fLogin_Paint(object sender, PaintEventArgs e)
  {
      drawBorder(System.Drawing.Color.Red);
  }
  </code></pre>
  
  <p>and then call it again whenever I need (eg on a button click) with a different color argument. This works on my single form. But I need this in every form.</p>
  
  <p>So <strong>my second question is</strong> do i need to create a _paint event in every form for my solution to work?</p>
  
  <p>I have also thought that i could create a new type of Form, inheriting the default form, add the _paint event in that form and define all my forms as this type. Which is the best approach?</p>
  
  <p><strong>My last question is</strong>: When i call my function more than once, what I really do is drawing a new rectangle on top of the previous one. Should i use</p>
  
  <pre><code>this.Invalidate();
  </code></pre>
  
  <p>before changing the color of my border?</p>