Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Determining if the .Net IDE is in Design Mode

[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

[Advertisement]

Comments

jokiz wrote re: Determining if the .Net IDE is in Design Mode
on 09-26-2006 7:43 AM

i have been a winforms component developer and the DesignMode suffice my needs.  you just can't query it in the component's constructor.  the other issue is when you used the component in a usercontrol thereby the usercontrol is in designmode, the component is in runtime mode.  i think i resolved to checking for the DesignMode property of the parent control up the hierarchy one time just to handle such scenario.

Adrian Banks wrote re: Determining if the .Net IDE is in Design Mode
on 12-14-2006 8:38 PM

Process.GetCurrentProcess().ProcessName can be a very slow call in a debug build because of the way process information is obtained. It is better to use the CodeBase property of the result of Assembly.GetExecutingAssembly().

I encountered this several months back: http://www.adrianbanks.co.uk/?p=11

3f blog » Blog Archive » Winforms y DesignMode wrote 3f blog » Blog Archive » Winforms y DesignMode
on 02-28-2008 4:24 PM

Pingback from  3f blog  » Blog Archive   » Winforms y DesignMode

brandon wrote re: Determining if the .Net IDE is in Design Mode
on 10-31-2008 4:05 PM

worked great...

i had custom usercontrols nested several levels deep and the DesignMode property would always return false on the inner controls - caused VS to completely freeze up everytime i tried to use the outer control - very frustrating.

thanks for the help...

y2kstephen wrote re: Determining if the .Net IDE is in Design Mode
on 04-07-2009 2:19 PM

thx a lot!!!

work like a charm in Web Forms too~

y2kstephen wrote re: Determining if the .Net IDE is in Design Mode
on 04-07-2009 2:21 PM

thx a lot!!!

work like a charm in Web Form too~

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)