Design Mode Detection

snarfblam

Mega-Ultra Chicken
Joined
Jun 10, 2003
Messages
1,832
Location
USA
User Rank
*Expert*
The DesignMode property of the Control class is meant to be testable to see if the control is in design mode, allowing the programmer to produce different behavior at design time than at runtime. Unfortunately, the behavior of this property is particularly unreliable.

It certainly has frustrated me at times and Im sure that it has frustrated others, so I did a quick google search and found a few work-arounds on a blog and another blog.

  • Test the value of GetService(typeof(IDesignerHost)) for null within a component. A null value indicates that the component is not hosted in a designer. This method did not work for me consistently.
  • Check System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime. A true value indicates design mode. I havent tried this method.
  • Check that the process hosting the component is devenv. This is a less solid option because it wont work properly in certain circumstances, for example within VS add-ins or #Develop.
  • Add a public static field to a class (ideally the Program class) named DesignMode that is initialized to true. In your applications Main() set the fields value to false. Because your applications Main method is not run within the designer, DesignMode will return true in the designer and false outside the IDE. This method is my preferred method, but isnt available with an VB program that doesnt have a Main() method.
 
Last edited by a moderator:
Back
Top