all groups > dotnet windows forms designtime > december 2007 >
You're in the

dotnet windows forms designtime

group:

Windows Form DesignTime/Mode Detection


Windows Form DesignTime/Mode Detection Laser Lu
12/15/2007 3:43:35 PM
dotnet windows forms designtime:
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

Re: Windows Form DesignTime/Mode Detection Bob Powell [MVP]
12/15/2007 7:25:28 PM
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.


[quoted text, click to view]
Re: Windows Form DesignTime/Mode Detection Laser Lu
12/16/2007 11:30:41 AM
Thank you, Bob! It works!:)

[quoted text, click to view]

AddThis Social Bookmark Button