In a System.Windows.Forms.Control and anything that inherits from it, you have a nifty DesignMode property. Only this property is only filled when it has been created, so it can't be used in a custom control's constructor. Also sometimes you want to set readonly (static/shared) properties or fields to a value depending on the design or live environment (eg, a sqlconnection-string is mapped to the design database when run from designer and to the production dbase otherwise)
For a lot of these options you can use precompiler constants to check the DEBUG enviroment which would be the most efficient way, but sometimes you want to be able to have users use the debug versions (where of course you relay the debug info to yourself), but still with the production settings. In other words, how to check if the current code is running (or started) from the designer?
Thanks to John Liu (see comments) who provided a great way to check if the code currently is running in design mode:
System.ComponentModel.LicenseManager.UsageMode
The above will do all you need to know that you're running in Design mode. However this post is one big mess of badly formulated goals. One of the goals was to know if the executing code was started in debug mode from within visual studio. In other words ran by pressing F5 in visual studio.
Back in the old days ;-) in vs2002 and vs2003 you could see if the app was started from a designer with:
VS2003 version:
///
///
True als de code wordt uitgevoerd vanuit de designer
/// ,anders false
///
public static readonly bool StartedFromDesigner=
Application.ExecutablePath.ToLower()
.IndexOf("devenv") != -1;
. . .
Off course, with this simple check you can not create applications that include devenv in its name, but never really saw that as a problem.
In the 'new' age however, the app isn't exactly started from visual studio, but rather a special version of the executable is created (appname.vshost.exe) that is run instead of the 'normal' executable. (If I'm formulating this wrong, please feel free to correct me).
One of the nice things is, that reading out Application.ExecutablePath gives back the eventual filename, not the vshost one. However no vshost and no devenv makes the above trick impossible.
Luckily, as it turns out, now it can be done much easier:
VS2005 version:
public static readonly bool StartedFromDesigner = AppDomain.CurrentDomain.FriendlyName.EndsWith(".vshost.exe");
. . .
NB: there's probably a better way of checking whether the code is run from the designer, so if anyone knows of a more direct way, please enlighten us :) In the meantime, this'll work for me.