Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Possible Bug in Visual Studio rendering UserControls in a parent form:
 

Possible Bug in Visual Studio rendering UserControls in a parent form:

I have a UserControl that has an Load() event inwhich it references a static object that is assigned at run-time. This means the object is null at design time. My understanding is that the Load() event is not called in DesignMode. At least that is how it appears for a Form class.

I have a repro projectfor this case and I'd like to report this bug to Microsoft. Where should I do this and how would I upload the zip fileof the project?

Thanks!

aztec2_step  Tuesday, January 09, 2007 5:30 PM

Hi aztec,

While you are correct that the Form's Load method is not executed at design time, both UserControls and Inherited Forms are actually being run in the designer. This means that a variety of runtime events can be ran in the designer including Load, Paint, Resize, etc. If you want to avoid referencing the object in the designer, you can use the DesignMode property. This property will be true when the control is running in the designer, and false during runtime. An example on how to use it is:

private void UserControl1_Load(object sender, EventArgs e)

{

if (this.DesignMode)

label1.Text = "Design-Time";

else

label1.Text = "Runtime";

}

Where the text of label1 on the UserControl will display if it is in the designer or not.

Let me know if this helps!

Thanks,

Scott Morrison

Program Manager

Windows Forms

Scott_Morrison  Wednesday, January 17, 2007 1:52 AM

Hi aztec,

While you are correct that the Form's Load method is not executed at design time, both UserControls and Inherited Forms are actually being run in the designer. This means that a variety of runtime events can be ran in the designer including Load, Paint, Resize, etc. If you want to avoid referencing the object in the designer, you can use the DesignMode property. This property will be true when the control is running in the designer, and false during runtime. An example on how to use it is:

private void UserControl1_Load(object sender, EventArgs e)

{

if (this.DesignMode)

label1.Text = "Design-Time";

else

label1.Text = "Runtime";

}

Where the text of label1 on the UserControl will display if it is in the designer or not.

Let me know if this helps!

Thanks,

Scott Morrison

Program Manager

Windows Forms

Scott_Morrison  Wednesday, January 17, 2007 1:52 AM

Thanks, I did stumble on (this.DesignMode) on google and that solved the problem just as you documented. What made it so difficult to troubleshoot was that it wasn't reporting what line would cause the problem. When you think about it, there can be some very deep code that requires a connection to a database to get table values.In this case, I was getting database errors while there is no code in the UserControl that connects to a database.

Basically, I'm venting a little. Note also this can create a bug thatexist only in design-time (hindering development where you could not step through the program) while working perfectly in run-time. That's aggravating!

This might be an issue of "Discoverability" for Visual Studioor maybe I am very blind... I don'tknow, but thanks for the reply in the forum.

aztec2_step  Wednesday, January 17, 2007 2:11 AM

You can use google to search for other answers

Custom Search

More Threads

• Implementing hidden "content panel"...
• how print datagridview directly?
• listbox ownerdraw doesn't redraw when multiselect is on
• Image size in TreeNode
• Treenode Editor Question
• None square window in C#
• Add a control plus its designer
• Emitting design-time messages / warnings
• Mdichild Form (No Border) DropShadow
• how to create simple IDE? - from Where to start ?