tags:

views:

412

answers:

3

Following-on from this question, is it possible to detect whether one is in design or runtime mode from within an object's constructor?

I realise that this may not be possible, and that I'll have to change what I want, but for now I'm interested in this specific question.

A: 

Are you looking for something like this:

public static bool IsInDesignMode()
{
    if (Application.ExecutablePath.IndexOf("devenv.exe", StringComparison.OrdinalIgnoreCase) > -1)
    {
        return true;
    }
    return false;
}

You can also do it by checking process name:

if (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv")
   return true;
Jarek
A: 

You should use Component.DesignMode property. As far as I know, this shouldn't be used from a constructor.

Ula Krukar
This doesn't work when your control is inside of another control or form that is being designed.
Eric
+4  A: 

You can use the LicenceUsageMode enumeration in the System.ComponentModel namespace:

bool designMode = (LicenseManager.UsageMode == LicenseUsageMode.Designtime);
adrianbanks
That works nicely, thanks.
nwahmaet