65.9K
CodeProject is changing. Read more.
Home

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

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1 vote)

Feb 16, 2010

CPOL
viewsIcon

9620

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: if (this.Site.DesignMode) //Do stuff [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:
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.