In .Net Windows Forms, any form or control that derives from a Component has a DesignMode property that tells you whether the control is being rendered in design mode in Visual Studio. This is useful if your control has different behaviour at runtime than at design time (for example, if you show a connection dialog when a form opens but you don’t want it to happen in design mode inside Visual Studio).

The DesignMode property has its quirks though. Using Lutz Roeder’s Reflector reveals the implementation of the DesignMode property:

protected bool DesignMode
{
    get
    {
        if (this.site != null)
        {
            return this.site.DesignMode;
        }
        return false;
    }
}

This shows that design time support is not hooked up until the control is sited. Siting happens after the control is created, but before any properties are set, so if you check the DesignMode property in the constructor of a control, it will always be false.

There is also a bug with the DesignMode property whereby a custom control inside a custom control will always report its DesignMode property as false. Microsoft has more details of this in knowledge base article KB839202 and in their Visual Studio feedback website.

A workaround to both of the above problems it to use the following:

Application.ExecutablePath.ToLower().IndexOf("devenv.exe") > -1

A bit crude, but it works.


Categories
Tags