DesignMode + UserControls = HELL!!!
Today I had to work with some usercontrols in our project. The problem was that we do some database access from within the constructor. For all this to work well everything needs to be in the right place and configured correctly which is not the case in design mode. So when I tried to open a user control at design time I got various errors because there were exceptions accessing the database and so on… You get the point.
I wanted to solve this by adding the if Not Me.DesignMode then statement so the database code would not be called at design time. I started replacing all the code in the user controls only to discover that it doesn’t work.
What was the problem? Well it seems that when you have a user control the DesignMode property is not reliable. First off all its always false in a constructor or any code called from within the constructor. The false or true value for the DesignMode property is set when the control is sited (ISite interface) the only problem is that this happens after construction.
An other problem is nested controls. When you have a user control in an other user control or form and you debug it at design time the nested control will always indicate that it is not in DesignMode. Pretty annoying,no?
After some searching on the web and having viewed multiple solutions I found a solution that will solve in my case both problems mentioned above.
It has nothing to do with the control or it’s DesignMode property. I found that in the namespace System.ComponentModel there is a class called LicenseManager that has a property UsageMode. This property has got 2 possible values Runtime and Designtime. You can use this instead of the DesignMode property to check if you are running in design or at runtime because it will return you the correct state already in constructor code. An other advantage is that it can be used in nested user controls because it is not dependent on the control to set the correct value this way you can also use it correctly there.
Nice isn’t it? For me it does the trick, it might be still possible that there are some issues with this way of working, but I haven’t experienced any problems yet…

Daniel said,
August 27, 2007 at 1:23 pm
Hi!
I googled for a hint in how to detect design mode in non graphical components and found your LicenseManage tip. Great! It works perfectly, thanks!
Greetings
Daniel
Ashutosh said,
December 11, 2007 at 6:14 am
May all your wish come true!
Ashutosh
AdamT said,
May 2, 2008 at 1:46 pm
Good piece, helped me, thanks
Adam,
Carl Scarlett said,
May 15, 2008 at 3:30 am
Mate, this is an awesome tip.
Thank you so much!
Jurgen said,
May 30, 2008 at 1:02 pm
Very good tip!!
Paul said,
May 30, 2008 at 2:44 pm
Great, helped me!
Ant said,
September 25, 2008 at 12:35 pm
There is no LicenseManager in Compact Framework (((
Allan said,
October 7, 2008 at 2:50 pm
Very good tip, thank you
AdrianB said,
October 7, 2008 at 4:53 pm
Very useful, thanks a lot!
Regards,
Adrian
cht said,
December 16, 2008 at 4:39 pm
helped me al lot, thanks.
cht