Click here to Skip to main content
11,411,840 members (51,315 online)
Click here to Skip to main content

Detecting the IDE from a User Control

, 29 Nov 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
This article describes a method to detect the IDE and/or the debugger in the code-behind of a User Control.

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;    // Global in scope
private bool gblRunModeIs_DesignMode = false;   // Global in scope

protected override void OnLoad(EventArgs e)
    // TODO:  Add TdhListViewCtl.OnLoad implementation
    base.OnLoad (e);

    gblRunModeIs_DebugMode = System.Diagnostics.Debugger.IsAttached;
    gblRunModeIs_DesignMode = false;
    //    string dummy = System.Reflection.Assembly.GetEntryAssembly().Location.ToString();
    //    string dummy = System.Reflection.Assembly.GetEntryAssembly().FullName;
    //catch (System.NullReferenceException ex)
    //    gblRunModeIs_IDE_Logic = true;
    // A much better method for setting 'gblRunModeIs_DesignMode'
    // Determine whether the class is in DesignMode
    // by examining the .ProcessName of the current Process
    if (System.Diagnostics.Process.GetCurrentProcess().ProcessName.ToLower() == "devenv")
        gblRunModeIs_DesignMode = true;

    #region Special IDE "eye-candy" code
    if (gblRunModeIs_DesignMode)     // Execute code from the IDE at design-time 
    {                // (perhaps supplying the programmer visual feed-back)
        SomeMethod(someArgument);    // Execute some specialized code
    if (gblRunModeIs_DesignMode      // Execute code from the IDE at design-time
    || gblRunModeIs_DebugMode        // or when executing in Debug mode.
        OtherMethod(otherArgument);// Execute some specialized code

private int _SomeProperty = -1;
public int SomeProperty
    get { return _SomeProperty; }
        _SomeProperty = value; 
        if (!lRunModeIs_DesignMode)
        // bypass code that isn't to execute at design-time
            // some code


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author


United States United States
No Biography provided

Comments and Discussions

General5 PinmvpJohn Simmons / outlaw programmer14-Dec-08 6:43 
AnswerRe: 5 PinmemberIlíon14-Dec-08 17:41 
GeneralUpdated for VS2008 Pinmemberkaborka1-May-08 8:57 
GeneralRe: Updated for VS2008 PinmemberIlíon1-May-08 9:06 
GeneralRe: Updated for VS2008 PinmvpJohn Simmons / outlaw programmer14-Dec-08 6:41 
GeneralRe: Updated for VS2008 PinmemberIlíon14-Dec-08 17:45 
GeneralRe: Updated for VS2008 PinmvpJohn Simmons / outlaw programmer15-Dec-08 0:18 
GeneralDownload code example PinmemberAmR EiSa29-Nov-06 0:38 
GeneralRe: Download code example [modified] PinmemberIlíon29-Nov-06 2:09 
GeneralRe: Download code example PinmemberLazal15-Jan-09 10:34 
GeneralRe: Download code example PinmemberIlíon15-Jan-09 11:23 
QuestionWhy not use DesignMode? Pinmembereligazit7-Jul-06 22:20 
AnswerRe: Why not use DesignMode? PinmemberIlíon8-Jul-06 18:42 
Ignorance on my part.

Thanks for your note (even if I am now feeling embarrassed at my ignorance); for what I'm trying to do, the .DesignMode property is just what I need.

I've seen the property in relation to various classes. I'm fairly sure I looked up and read the documentation on it months ago (out of mere curiosity at the time) ... and forgot all about it when I needed to make use of it.

So, this article is mostly a waste of time ... unless one needs to know in the Constructor that the control is in design mode, or unless the component doesn't have an 'ISite' associated with it.
GeneralRe: Why not use DesignMode? Pinmembernorm .net29-Nov-06 4:14 
GeneralRe: Why not use DesignMode? PinmemberIlíon27-Oct-07 11:43 
GeneralRe: Why not use DesignMode? PinmvpJohn Simmons / outlaw programmer14-Dec-08 6:39 
AnswerRe: Why not use DesignMode? Pinmembertonyt9-Jul-06 23:22 
GeneralRe: Why not use DesignMode? Pinmembereligazit9-Jul-06 23:32 
GeneralRe: Why not use DesignMode? PinmemberIlíon10-Jul-06 9:08 
AnswerRe: Why not use DesignMode? PinmvpJohn Simmons / outlaw programmer14-Dec-08 6:38 
GeneralRe: Why not use DesignMode? PinmemberIlíon14-Dec-08 17:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150414.5 | Last Updated 29 Nov 2006
Article Copyright 2006 by Ilíon
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid