[NOTE: Before you read this, please understand I am a WinForms
developer so I can only tell you that I know the code in this article works on WinForms.
I assume it will work for WebForms, but not
sure.]
How many times have you been working on a WinForms app writing
some logic in the form to handle events, work with objects, etc and when you switch
over to ‘Design View’ all you see is a White form with an Exception message? Now normally this message is the dreaded ‘Object
not instance of an object message’. What
you have just encountered is NOT uncommon to everyone one else. Your WinForm application just tried to access an
object before you had an instance of it created (maybe this is valid because it
is a global object or is class level object that has not been created yet).
Well, lucky for you Microsoft has created this create
property on its component object called DesignMode. However, if you have ever tried to use it you
will get mixed results. The really good
news for you is this, you can write your own DesignMode check with a few lines
of code.
Below is the code I use to check for Design Mode
public static bool
IsInDesignMode()
{
bool returnFlag = false;
#if DEBUG
if ( System.ComponentModel.LicenseManager.UsageMode ==
System.ComponentModel.LicenseUsageMode.Designtime )
{
returnFlag = true;
}
else if ( Process.GetCurrentProcess().ProcessName.ToUpper().Equals(
"DEVENV" ) )
{
returnFlag = true;
}
#endif
return returnFlag;
}
Now if you
notice, I wrap my main check in #if DEBUG because I don’t care about this if I am
in Release mode.
There are 2
checks above just to be double safe, In the past I have only used the Process.GetCurrentProcess().ProcessName.ToUpper().Equals(
"DEVENV" ) check and
that has worked just fine, but I ran across the LicenseManager check and threw that
in for good measure.
If anyone else
has a better way of doing this, please let me know.
Posted
09-25-2006 12:58 PM
by
Derik Whittaker