3

Code below is not working as expected to detect if it is in design mode (VS.Net 2003 - Control Library):

if (this.Site != null && this.Site.DesignMode == true)
{
// Design Mode
}
else
{
// Run-time
}

It is used in a complex user control, deriving from another user control and including other user controls on it.
Is there another way to detect design time in a VS.NET 2003 or what is the problem with the code above?

Kent Fredric
  • 56,416
  • 14
  • 107
  • 150
rovsen
  • 77
  • 1
  • 7

2 Answers2

6

DesignMode won't work from inside a constructor. Some alternatives (not sure if they work in 1.1) are

if (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime)

or

call GetService(typeof(IDesignerHost)) and see if it returns something.

I've had better luck with the first option.

Nicholas Piasecki
  • 25,203
  • 5
  • 80
  • 91
  • if (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime) worked. thanks.. – rovsen Jan 30 '09 at 12:30
0

I guess you could use HttpContext.Current == null as described at http://west-wind.com/weblog/posts/189.aspx

In .Net 2.0 there's Control.DesignMode (http://msdn.microsoft.com/en-us/library/system.web.ui.control.designmode.aspx). I guess you have a good reasons to stay on VS 2003 though, so upgrading might not be an option for you.

Update If you are doing Winforms, Component.DesignMode (http://msdn.microsoft.com/en-us/library/system.componentmodel.component.designmode.aspx) is the right way to check. Though, if this.Site.DesignMode doesn't work properly, Component.DesignMode might not work as well, as it does exactly the check you are doing (Site != null && Site.DesignMode).

This might be a long shot, but are you sure that your base control does not override the Site property?

Franci Penov
  • 74,861
  • 18
  • 132
  • 169
  • For HttpContext; it is winform application, not web. For staying on VS 2003; for now it is not an option unfortunatelly. – rovsen Sep 28 '08 at 02:00
  • Component.DesignMode supported in: 3.5, 3.0, 2.0. The software is running on Framwrork 1.1. Thanks for your interest. – rovsen Sep 28 '08 at 09:47
  • According to the MSDN page of Component.designMode, that property has existed since 1.0. – Franci Penov Sep 28 '08 at 10:30