How Do I Detect Design-Time Vs Run-Time in a .NET Control?

When you're designing custom controls in Visual Studio .NET, you'll often need to distinguish between design-time and run-time. You could, for example, load a static image, or create a made-up DataSource at design-time while you use a real image or DataSource at run-time. The following snippet allows you to determine whether you're currently in design-time or not.
if (this.Site != null && this.Site.DesignMode == true)
{
  // Design Mode
}
else
{
  // Run-time
}

Where to Next?

Wonder where to go next? You can read existing comments below or you can post a comment yourself on this article .


Consider making a donation
Please consider making a donation using PayPal. Your donation helps me to pay the bills so I can keep running Imar.Spaanjaars.Com, providing fresh content as often as possible.



Feedback by Other Visitors of Imar.Spaanjaars.Com

On Monday, June 26, 2006 8:20:00 PM Kris Krause said:
Thanks for the tip.

Works the same way with WinForm controls .DesignMode
On Sunday, February 10, 2008 7:15:30 PM Eric Sierp said:
Good suggestion.  What about detecting Design Time in the constructor of a control though?  I have a control for Windows Mobile that needs to access System Properties such as PowerBatteryStrength upon creation so that it can subscribe to the change events.  Site, and therfore DesignMode, is not available in the constructor of a control.  Does anyone have any suggestions for how to do this?

Thanks
On Sunday, February 10, 2008 9:39:54 PM Imar Spaanjaars said:
Hi Eric,

Sorry, no suggestions. It's been a while since I did anything "mobile"...

Imar
On Tuesday, November 25, 2008 3:38:18 PM Michael Finlan said:
Hi Imar,

I appreciate this article. It has been a long time I could never find a way to do this and believe me I looked.

Finally!

Brs,

Michael
On Saturday, January 10, 2009 11:20:43 PM Eugen said:
I have a post about this problem (including detecting the design mode in a usercontrol constructor). Please read this article, maybe you will find it useful: http://dotnetfacts.blogspot.com/2009/01/identifying-run-time-and-design-mode.html

Talk Back! Comment on Imar.Spaanjaars.Com

I am interested in what you have to say about this article. Feel free to post any comments, remarks or questions you may have about this article. The Talk Back feature is not meant for technical questions that are not directly related to this article. So, a post like "Hey, can you tell me how I can upload files to a MySQL database in PHP?" is likely to be removed. Also spam and unrealistic job offers will be deleted immediately.

When you post a comment, you have to provide your name and the comment. Your e-mail address is optional and you only need to provide it if you want me to contact you. It will not be displayed along with your comment. I got sick and tired of the comment spam I was receiving, so I have protected this page with a simple calculation exercise. This means that if you want to leave a comment, you'll need to complete the calculation before you hit the Post Comment button.

If you want to object to a comment made by another visitor, be sure to contact me and I'll look into it ASAP. Don't forget to mention the page link, or the QuickDocId of the document.

For more information about the Talk Back feature, check out this news item.