Creativity, Innovation... Failure



Detecting design mode

Lets say you've just created a user control or customized an existing control from the .Net Framework and you want the bahavior to be different when you're in design mode vs execution mode. How can you do it?

Solution #1: DesignMode Property 

The first an very intuitive approach is to use the DesignMode property of your control. The problem is that it will always return false if it has a parent... not so great

Solution #2: Design Mode Property with a twist 

The solution then is to go up the control hierarchy and find the parent with the right value!
The resulting code will look a little like this:

public bool IsDesignMode ()
{
    Control instance = this;
    while (instance != null)
    {
        if (instance.Site == null) return false;
        if (instance.Site.DesignMode) return true;
        instance = instance.Parent;
    }
    return false;
}

Using this method can prevent errors where the GUI does not draw properly in design mode because it tries to access data (i.e. database connection)
The biggest limitation is that you can't call the method from the constructor because the parent may not be set yet!

Of course there are other solutions but they are not as elegant. Here's one of them:

bool designMode = Application.CompanyName == "Microsoft Corporation";

This solution works from the constructor, so it does have its advantages...

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Related posts

Comments

July 12. 2008 11:42

pix98

The line of code...
bool designMode = Application.CompanyName == "Microsoft Corporation";
How does it work? It it a third solution or a workaround to the redraw problem?

pix98

July 12. 2008 16:36

marivet

bool designMode = Application.CompanyName == "Microsoft Corporation";

This is a third solution to know if you are in design mode. Since the second one doesn't work in the constructor, I mention this one so you can check if you are in design mode from the constructor if necessary (No limitations.. but it's just plain ugly!!)

marivet

November 16. 2008 01:37

Morten Grøtan

Thanks a bunch! You saved my day. I've been poking around, trying to use various sorts of FindControl attempts to locate the "ancestor" control (top-most parent besides the hosting form itself), but to no avail.
The other "solutions" have all been tied to specific types of controls or specific IDs.

Your solution is generic and straight-forward, and most importantly: It works!

Morten Grøtan

August 2. 2009 20:57

Voguishchic

Great post! Keep up the good work!

Voguishchic

November 4. 2009 08:56

paydayloans

I just hope to have understood this the way it was meant

paydayloans

November 9. 2009 01:10

cash advance

Nice post and very helpfull

cash advance

November 10. 2009 14:09

payday loans

Just try to smile for about 2-3 mins then you can get back to work

payday loans

December 5. 2009 18:50

payday loans

Yea nice Work !Laughing

payday loans

December 6. 2009 19:28

payday loans

thanks! very helpful post!! like the template btw ;)

payday loans

December 31. 2009 01:59

ubuntu blog

Hello blogger, good morning. Interesting article. You have gained a new reader. Please continue this great work and I look forward to more of your amazing articles. Cheers, Russel.

ubuntu blog

January 7. 2010 06:33

payday loans

Like your writing! Still you can do some things to improve it.

payday loans

January 8. 2010 00:44

ironing board covers

I am not new to blogging and actually value your web site. There is much innovative content that peaks my interest. I am going to bookmark your site and keep checking you out.

ironing board covers

January 8. 2010 14:55

slow computer repair

Advantageously, the post is actually the best topic on the best registry cleaner in 2009. I concur with your conclusions and will eagerly look forward to your future updates. Just saying thanks will not just be enough, for the phenomenal lucidity in your writing. I will right away grab your rss feed to stay privy of any updates.

slow computer repair

January 8. 2010 18:09

Hipolito M. Wiseman

First Off, let me commend your clearness on this subject. I am not an expert on this matter, but after learning your article, my understanding has developed considerably. Please tolerate me to take hold of your rss feed to remain in touch with any future updates. Delightful job and will extend it on to admirers and my followers.

Hipolito M. Wiseman

January 20. 2010 16:39

turystyczne pieninach

I moldiness say that journal is holographic in very informatory way. Congralutaltion for author for discriminating system and riveting articles.I outlook to see solon bully articles same this one

turystyczne pieninach

January 23. 2010 13:38

Waschbeckenunterschrank

Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It's always nice when you can not only be informed, but also entertained! I'm sure you had fun writing this article.

Waschbeckenunterschrank

January 25. 2010 08:39

website design and hosting

Hey mate, your web page's theme is glorious and loving it. Your blog posts are pretty cool. Please continue this great work. Best Regards!!!!

website design and hosting

January 25. 2010 16:49

rovin net business services

Aw, this was a really great post. In theory I'd like to write like this too - taking time and real effort to make a good article... but what can I say... I procrastinate alot and never seem to get something done

rovin net business services

January 26. 2010 00:58

dog crate training

This blog is awesome. I'm glad I've recovered this diary.

dog crate training

January 26. 2010 01:30

Kre Alkalyn Pro

I really enyojed this brilliant blog. Please keep them coming. Greets!!!

Kre Alkalyn Pro

January 28. 2010 00:57

White

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me.

White

January 28. 2010 17:02

Sauerkirschen

strange this post is totaly irrelevant to the search query I entered in google but it was listed on the first page.

Sauerkirschen

February 12. 2010 11:53

payday loans

Problems are not stop signs, they are guidelines.

payday loans

March 2. 2010 08:40

ucvhost

I admire the valuable information you offer in your articles. I will bookmark your blog and have my children check up here often. I am quite sure they will learn lots of new stuff here than anybody else!

ucvhost

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

March 11. 2010 14:04