Hi,

I get the following error when I Rebuild the Solution in Visual Studio:

The control WindowsFormsApplication1.MyPanel has thrown an unhandled exception in the designer and has been disabled

Here is what I did to get this error:
1) Started a new project and added a new User Control (MyControl) to the solution.
2) Added MyControl in the designer to the Form.
3) Added the following call to Init() method that I implemented in MyControl:

partial class Form1
{
    ....
    private void InitializeComponent()
        {
            this.myControl1 = new WindowsFormsApplication1.MyControl();
            this.SuspendLayout();
            // 
            // myControl1
            // 
            this.myControl1.Location = new System.Drawing.Point(73, 38);
            this.myControl1.Name = "myControl1";
            this.myControl1.Size = new System.Drawing.Size(150, 150);
            this.myControl1.TabIndex = 0;
            [b]this.myControl1.Init();[/b]
            // 
            // Form1
            // 
            ....

4) Added the following code to MyControl class:

namespace WindowsFormsApplication1
{
    public partial class MyControl : UserControl
    {
        private Bitmap myImage;
        private Graphics g;

        public MyControl()
        {
            InitializeComponent();
        }

        public void Init()
        {
            myImage = new Bitmap(Width, Height);
            g = Graphics.FromImage(myImage);
            g.Clear(Color.Sienna);
            g.DrawEllipse(Pens.Red, 0, 0, 50, 50);
            g.Dispose();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.DrawImageUnscaled(myImage, 0, 0);
        }
    }
}

When I run the application, everything works fine.
But when I "Rebuild Solution" I got the "unhandled exception in the designer" error.

Please help to understand why this is happening.

Thanks a lot !

By the way, the "Unhandled exception in the designer" error message pointing to this line: e.Graphics.DrawImageUnscaled(myImage, 0, 0); saying that myImage is null.
Why is that ??

Try taking the call out of InitializeComponent as I'm not sure it's considered part of myControl1. Try calling just plain Init() in the constructor of MyControl after InitializeComponent(). It's balking about that line because if Init() is not being called correctly the image is indeed going to be null.

You should initialize your control in the control's Load event. There is no sense in loading a picture in your control until the window handle is created and you're about to display the control.

You can also test if you're in design-time:

System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))

Try taking the call out of InitializeComponent as I'm not sure it's considered part of myControl1. Try calling just plain Init() in the constructor of MyControl after InitializeComponent(). It's balking about that line because if Init() is not being called correctly the image is indeed going to be null.

I tried to call Init() in MyControl's constructor as you suggested, but it is not good enough because InitializeComponent() of the form is generated like this:

private void InitializeComponent()
        {
            [b]this.myControl1 = new WindowsFormsApplication1.MyControl();[/b]
            this.SuspendLayout();
            // 
            // myControl1
            // 
            this.myControl1.Location = new System.Drawing.Point(39, 33);
            this.myControl1.Name = "myControl1";
            [b]this.myControl1.Size = new System.Drawing.Size(225, 189);[/b]
            this.myControl1.TabIndex = 0;
            // 
            // Form1
            // 
            ......
        }

Particularly, MyControl's constructor is called before MyControl's size is defined.
This means that Width and Height inside Init() are not updated enough.

Any other ideas ?

You should initialize your control in the control's Load event. There is no sense in loading a picture in your control until the window handle is created and you're about to display the control.

You can also test if you're in design-time:

System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))

I also tried to call Init() in MyControl's Load event. It works :)
The only problem is that in my real application MyControl inherits from Panel rather than UserControl, thus it does not have the Load event. When should I call Init() in this case ?
Thanks !

I tried to call Init() in MyControl's constructor as you suggested, but it is not good enough because InitializeComponent() of the form is generated like this:

Apologies for leading you down the wrong path. I guess I'm not able to appreciate the dependencies of your code.

in my real application MyControl inherits from Panel rather than UserControl

I'm not sure I understand, so this is just a model of your code? If so you should put up the relevant sections of what you are actually working on. Otherwise we're chasing 2 different targets ;)

My two cents:
I smell bad code on line 16.

g = Graphics.FromImage(myImage);

Try putting a breakpoint there and posting the stacktrace from there.

My two cents:
I smell bad code on line 16.

g = Graphics.FromImage(myImage);

Try putting a breakpoint there and posting the stacktrace from there.

Why do you think this line is bad ?

Anyway, I solved the issue by checking in OnPaint if myImage is null first. If yes, I call Init().

Thank you all !

I was suspecting that it could refer to null, that's why I asked for stacktrace.
It seems you've solved it.

Yes, Thanks anyway !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.