2

Pardon me for this long story, but I think the question merits it.

I have a custom control that I made, which had it's own overridden OnPaintBackground method which used a member Brush and Pen. I was creating it like this:

CustomControl c = new CustomControl();
c.Parent = someParent;

and the constructor was just the default one with some added stuff for creating brushes and pens and stuff for the background.

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:

public CustomControl(Control parent) {
    InitializeComponent();
    Parent = parent;
    // do a bunch of stuff like getting brushes, pens, etc for drawing the background
}

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 System.Drawing.dll and the backgrounds of all the panels inside my control were that red X 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.

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 OnPaintBackground that looked like this :

e.Graphics.DrawLine(bPen, x, y, Width, Height);

With the information Argument cannot be null. So I looked in the debugger window and found that not only was bPen null, but all my brushes and pens and everything was null even though the constructor was being called (verified with a MessageBox)!

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 Pens?

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249

2 Answers2

3

There's some useful answers to this question: 'UserControl' constructor with parameters in C#

Community
  • 1
  • 1
stuartd
  • 70,509
  • 14
  • 132
  • 163
2

C# has nothing against controls, it doesn't care. It's just a language. Usually you will want a default constructor because the designer relies on them. In fact, something like this works just fine for me:

public class MyControl : Control
{
    private Control _parent;
    public MyControl()
    {
        InitializeComponent();
    }

    public MyControl(Control parent) : this()
    {
        _parent = parent;
    }
}
vcsjones
  • 138,677
  • 31
  • 291
  • 286
  • yea and that's how I did it too to solve the problem, but why did my code fail to create a Pen in any other constructor than the default one? That's the question. – Seth Carnegie May 23 '11 at 16:10
  • 2
    @Seth windows forms is not calling your custom constructor but only the default one with no parameters, if you have the InitializeComponent only in the parametrized one, it's not ok. Also, from your custom one, is good practice to call the default one with : this(). – Davide Piras May 23 '11 at 16:14
  • @Davide how can it call the one with no parameters if it doesn't exist? I took the one with no parameters and _added a parameter_, so it didn't exist any more. Does C# or the forms designer magically create one even though it's not in my code? – Seth Carnegie May 23 '11 at 16:15
  • you have to create one, do like in this answer above yopu already got. – Davide Piras May 23 '11 at 18:44