Windows Form DesignTime/Mode Detection

L

Laser Lu

Hi, I'm a novice in .NET Windows Forms programming. Recently, I was stumbled
by a puzzle, regarding how to determine whether the code is currently
executing under design-time or not.
My code was written in C# under Visual Studio .NET 2003. In my project, I
just created a Windows Form as a base form, and then inherited another
Windows Form based on that one. Everything works fine. But, after I had
added some custom code into the base form's OnLoad event handler, the
problem occurs that the inherited form can no longer be openned in the
designer. And the designer says that "An error occurred while loading the
document".

Here is the skeleton code snippets:

public class MyBaseForm : System.Windows.Forms.Form
{
protected override void OnLoad(EventArgs e)
{
try
{
// some custom code was added here,
// which may be error prone at design time
...
}
catch
{
}

base.OnLoad (e);
}

}
public class InheritedForm : MyBaseForm
{
// this inherited form can not be loaded by the designer,
// if some error was thrown within the base form's code
...
}

So, the question is obvious. How to make a design mode detection and avoid
uncessary errors during design-time in my custom code?

Thanks,
Laser Lu
 
B

Bob Powell [MVP]

There is a property called, surprisingly, DesignMode which is true when an
object is hosted in the designer.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
L

Laser Lu

Thank you, Bob! It works!:)

Bob Powell said:
There is a property called, surprisingly, DesignMode which is true when an
object is hosted in the designer.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top