Purpose and use of the method/concept
The function of the code described here is to differentiate whether the code-behind of a User Control is executing from the IDE (i.e., in Design view) or in Debug mode, as opposed to being executed by the project's deployed .Exe file.
The reason for doing this is that there may be specific code in the control/class which either should not execute from the IDE or should execute only from the IDE. For instance, when setting a property of the control via the IDE, the property's {set}
accessor code is executed. Thus, absently detecting the IDE and controlling the code's execution, any and all code that will normally by executed at run-time will also be executed at design-time.
In testing this idea, I've found that it can be employed in the control's constructor method or in the control's OnCreateControl()
and OnLoad()
methods. I also saw that a control may not necessarily implement the OnLoad()
and OnCreateControl()
methods, but the constructor is always available as a fall-back.
I really detest that my implementation of this concept relies upon throwing (and catching) an exception. But, I found nothing on the internet concerning how to make this detection, and this was the best method I could come up with to accomplish the task.
Here is the sample code making use of this methodology:
private bool gblRunModeIs_DebugMode = false;
private bool gblRunModeIs_DesignMode = false;
protected override void OnLoad(EventArgs e)
{
base.OnLoad (e);
gblRunModeIs_DebugMode = System.Diagnostics.Debugger.IsAttached;
gblRunModeIs_DesignMode = false;
if (System.Diagnostics.Process.GetCurrentProcess().ProcessName.ToLower() == "devenv")
{
gblRunModeIs_DesignMode = true;
}
#region Special IDE "eye-candy" code
if (gblRunModeIs_DesignMode)
{
SomeMethod(someArgument);
}
if (gblRunModeIs_DesignMode
|| gblRunModeIs_DebugMode
)
{
OtherMethod(otherArgument);
}
#endregion
}
private int _SomeProperty = -1;
public int SomeProperty
{
get { return _SomeProperty; }
set
{
_SomeProperty = value;
if (!lRunModeIs_DesignMode)
{
}
}
}