How to prevent code from being executed by the Designer?

  • Thread starter Thread starter Fabio Cannizzo
  • Start date Start date
F

Fabio Cannizzo

How can I prevent a few lines of code contained in the constructor of a Form
from being executed by the Designer?

Thanks,
Fabio
 
Fabio,

When developing user controls or custom controls, I SOMETIMES use
this.DesignMode to prevent things from being created.

Why do you need it for a form?

Dave
 
How can I prevent a few lines of code contained in the constructor of
a Form from being executed by the Designer?

Check the DesignMode property of the form.

if (!this.DesignMode)
{
// Code here only executes when running, not in design mode
}

-mdb
 
Fabio,

Check the DesignMode property of the control that you have. If it
returns true, then you are in design mode.

This is inherited from Component.

Hope this helps.
 
Thanks to all of you guys.

In reality I need this in an object inherited from UserControl, not from
Form.

The constructor of my UserControl will create some unmanaged objects using
interops, and I suspect this is what is causing lots of problems with my VS.
See my other post just two hours ago: "Bad UserControl in toolbox".

Regards,
Fabio
 
Learn something new everyday.....

This article mentions what u said:
http://msdn.microsoft.com/library/d...en-us/dnwinforms/html/designtimedebugging.asp

Any class that derives from the Component class has a property called
DesignMode that is set to true when the code is executing within the
constructs of the Visual Studio .NET designer. So you have the option to
wrap code within an if statement. There's one more trick, however. The
DesignMode property isn't set to true in the constructor. Remember, there's
no real magic here. Visual Studio .NET creates your object as it parses the
InitializeComponent() method. Once the object is constructed, Visual Studio
..NET keeps track of the objects it creates.....

Microsoft needs to update the documentation for Component.DesignMode
Property:

http://msdn.microsoft.com/library/d...mponentmodelcomponentclassdesignmodetopic.asp

to reflect this information!!!


Dave
 
Back
Top