title: Custom Control Constructors
body: <p>Pardon me for this long story, but I think the question merits it.</p>
  
  <p>I have a custom control that I made, which had it's own overridden <code>OnPaintBackground</code> method which used a member <code>Brush</code> and <code>Pen</code>. I was creating it like this:</p>
  
  <pre><code>CustomControl c = new CustomControl();
  c.Parent = someParent;
  </code></pre>
  
  <p>and the constructor was just the default one with some added stuff for creating brushes and pens and stuff for the background.</p>
  
  <p>However, I thought it would be nicer to have a constructor that took the parent panel (since my control can't be used without a parent) so I added one parameter to the constructor:</p>
  
  <pre><code>public CustomControl(Control parent) {
      InitializeComponent();
      Parent = parent;
      // do a bunch of stuff like getting brushes, pens, etc for drawing the background
  }
  </code></pre>
  
  <p>Then when I ran my program, the first control was created and drawn fine. Then for all the controls after the first one, I was getting first chance exceptions in <code>System.Drawing.dll</code> and the backgrounds of all the panels <em>inside</em> my control were that red <code>X</code> that Winforms shows when it can't find an image or something. Remember though that the first instance of the control to be created was working perfectly.</p>
  
  <p>So I set my Visual Studio to break when any exceptions were thrown instead of just logging it, and it broke on a line inside my overriden <code>OnPaintBackground</code> that looked like this :</p>
  
  <pre><code>e.Graphics.DrawLine(bPen, x, y, Width, Height);
  </code></pre>
  
  <p>With the information <code>Argument cannot be null</code>. So I looked in the debugger window and found that not only was <code>bPen</code> null, but all my brushes and pens and everything was null even though the constructor was being called (verified with a MessageBox)!</p>
  
  <p>I eventually solved the problem by removing the parameter from the default constructor and adding another seperate constructor that took an argument, and calling the default ctor from that one. However, I would like to know, what does C# have against controls not having a default constructor that makes them not able to create stuff like <code>Pen</code>s?</p>