Click here to Skip to main content
15,881,248 members
Articles / Desktop Programming / Windows Forms
Alternative
Tip/Trick

Prevent code executing at Design Time in Visual Studio for Winforms Apps

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
18 Feb 2010CPOL 9.5K   3  
If you are designing in a WinForms component, which is anything that derives from Control at some point (Forms, UserControls, panels, etc), then you can use the following code:if (this.Site.DesignMode) //Do stuff[edit]The above will throw an exception if Site is not set (in...
If you are designing in a WinForms component, which is anything that derives from Control at some point (Forms, UserControls, panels, etc), then you can use the following code:

<br />
if (this.Site.DesignMode)<br />
   //Do stuff<br />


[edit]
The above will throw an exception if Site is not set (in most cases it is, ie the control is in a container control). However:

C
if (this.DesignMode)
   //Do stuff

Will return false if there is no container. This is an alternative (and much neater) method, but may give incorrect results in certain (rare) circumstances.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
President 6D Systems LLC
United States United States
I studied Software Engineering at Milwaukee School of Engineering for 2 years before switching to Management of Information Systems for a more business oriented approach. I've been developing software since the age of 14, and have waded through languages such as QBasic, TrueBasic, C, C++, Java, VB6, VB.NET, C#, etc. I've been developing professionally since 2002 in .NET.

Comments and Discussions

 
-- There are no messages in this forum --