How to prevent code from being executed by the Designer?

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
 
D

D. Yates

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
 
M

Michael Bray

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
F

Fabio Cannizzo

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
 
D

D. Yates

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
 

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