The DesignMode property does not always return the correct value, specially for nested controls or for child controls instantiated in their parent control’s constructors. One workaround is to check for LicenseManager.UsageMode and see if it’s equal to LicenseUsageMode.Runtime, but even that won’t work all the time. It will always return Runtime from event handlers and worker threads, so a more guaranteed approach is to see if the current process-name is devenv. Of course that’s a slightly heavier call so we should still check for LicenseManager.UsageMode first, and only check the process-name if we have to.
bool isDesignMode = LicenseManager.UsageMode == LicenseUsageMode.Designtime
|| Process.GetCurrentProcess().ProcessName.ToLowerInvariant().Contains("devenv");
I would like this article more if it explained what DesignMode, LicenseManager, and the rest of that stuff was. I guess this article is for someone previously familiar with those things.
Aah, thanks for this. I’d been using the LicenseManager way of checking for forever, but was frustrated when nested custom controls wouldn’t all report that it was in DesignTime.
That little devenv check is so stupidly nice!