471,350 Members | 1,566 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

How to prevent code from being executed by the Designer?

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

Thanks,
Fabio
Dec 28 '05 #1
7 8591
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Fabio Cannizzo" <fc*****************@london.edu> wrote in message
news:43**********************@reader3.news.tin.it. ..
How can I prevent a few lines of code contained in the constructor of a
Form from being executed by the Designer?

Thanks,
Fabio

Dec 28 '05 #2
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

"Fabio Cannizzo" <fc*****************@london.edu> wrote in message
news:43**********************@reader3.news.tin.it. ..
How can I prevent a few lines of code contained in the constructor of a
Form from being executed by the Designer?

Thanks,
Fabio

Dec 28 '05 #3
"Fabio Cannizzo" <fc*****************@london.edu> wrote in
news:43**********************@reader3.news.tin.it:
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
Dec 28 '05 #4
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. Yates" <fo****@hotmail.com> wrote in message
news:eH****************@TK2MSFTNGP09.phx.gbl...
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

"Fabio Cannizzo" <fc*****************@london.edu> wrote in message
news:43**********************@reader3.news.tin.it. ..
How can I prevent a few lines of code contained in the constructor of a
Form from being executed by the Designer?

Thanks,
Fabio


Dec 28 '05 #5
Since you are doing this work in a UserControl, you might find that you need
to move some of the code out of the constructor to get the DesignMode
property to function correctly:

http://groups.google.com/groups?as_q...e+gone+in+.Net
http://groups.google.com/groups?as_q=Design+Mode+-+My+Constructor+keeps+running+in+design!
"Fabio Cannizzo" <fc*****************@london.edu> wrote in message
news:43**********************@reader3.news.tin.it. ..
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. Yates" <fo****@hotmail.com> wrote in message
news:eH****************@TK2MSFTNGP09.phx.gbl...
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

"Fabio Cannizzo" <fc*****************@london.edu> wrote in message
news:43**********************@reader3.news.tin.it. ..
How can I prevent a few lines of code contained in the constructor of a
Form from being executed by the Designer?

Thanks,
Fabio



Dec 29 '05 #6
I also found this:
Debugging Design-Time Controls
http://msdn.microsoft.com/library/de...edebugging.asp

which u might find interesting.

Dave
Dec 29 '05 #7
Learn something new everyday.....

This article mentions what u said:
http://msdn.microsoft.com/library/de...edebugging.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/de...nmodetopic.asp

to reflect this information!!!
Dave
Dec 29 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Felix Wiemann | last post: by
1 post views Thread by Russ | last post: by
3 posts views Thread by Ryu | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.