Detecting Design-time in C#( Native DesignMode property not telling the full truth )

The DesignMode property for a UserControl object will show that it is in DesignMode only if the immediate parent is viewed in the IDE; if it is a grand child of the object that is being viewed in the IDE, then the DesignMode property will not be true.

The workaround:

///
/// Indicates if the current view is being utilized in the VS.NET IDE or not.
///
public new bool DesignMode
{
   get
      {
         return (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv");
      }
}

 

[ Via Mark Jordan's blog ]


 
 
 

2 Responses to “Detecting Design-time in C#( Native DesignMode property not telling the full truth )”

  1. Ayende Rahien
    29. April 2005 at 17:12

    Doesn’t work in Mono, #Develop, etc.

    Doesn’t work if you don’t have premissions to query the process parameters.

    If the grandchild thing is bothering you, do this:

    public override bool DesignMode

    {

    get {

    Control parent = Parent;

    while(parent!=null)

    {

    if(parent.DesignMode) return true;

    parent = parent.Parent;

    }

    return base.DesignMode;

  2. anonymous
    19. May 2005 at 17:55

    yes Design time need parent

    so use ISupportInitialize

    code now – think later

    public class foobar : UserControl, ISupportInitialize{

    #region ISupportInitialize Members

    private bool ISupportInitialize_bInit=false;

    public void BeginInit() {

    ISupportInitialize_bInit=true;

    }

    public void EndInit() {

    ISupportInitialize_bInit=false;

    if (this.DesignMode){

    foo();

    } else {

    bar();

    }

    }

    #endregion

    }

Leave a Reply