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 by Bob

Bob
Sat Dec 15 10:25:28 PST 2007

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.


"Laser Lu" <laser_lu@163.com> wrote in message
news:O54ix4uPIHA.4276@TK2MSFTNGP06.phx.gbl...
> 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 by Laser

Laser
Sat Dec 15 19:30:41 PST 2007

Thank you, Bob! It works!:)

"Bob Powell [MVP]" <bob@spamkillerbobpowell.net> wrote in message
news:9B1D1BAC-1496-4B64-B2A3-3AE637246EEE@microsoft.com...
> 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.
>
>
> "Laser Lu" <laser_lu@163.com> wrote in message
> news:O54ix4uPIHA.4276@TK2MSFTNGP06.phx.gbl...
>> 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
>>
>
>