Suddenelfilio\’s Weblog


Passionate about .net

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…


Filed under: .net 2.0, Visual Studio .Net 2005,

19 Responses

  1. Daniel says:


    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!


  2. Ashutosh says:

    May all your wish come true!


  3. AdamT says:

    Good piece, helped me, thanks


  4. Carl Scarlett says:

    Mate, this is an awesome tip.

    Thank you so much!

  5. Jurgen says:

    Very good tip!!

  6. Paul says:

    Great, helped me!

  7. Ant says:

    There is no LicenseManager in Compact Framework (((

  8. Allan says:

    Very good tip, thank you

  9. AdrianB says:

    Very useful, thanks a lot!


  10. cht says:

    helped me al lot, thanks.


  11. […] most often mentioned workaround is using the LicenseManager.UsageMode property. From all workarounds I’ve seen it is the most […]

  12. […] most often mentioned workaround is using the LicenseManager.UsageMode property. From all workarounds I’ve seen it appears the […]

  13. thed says:

    Works like a charm. Thanks!

  14. David Wendelken says:

    Check this link. Fixed my problem with design mode right away.

  15. The pings above seem to show the intention that LicenseManager.UsageMode is a good workaround. It is for a limited number of situations, but there are too many situations where it fails (to name one: compact framework). One solution that fits all is unfortunately not trivial. You can read about it on my blog, or just download the solution.

    — Abel —

  16. Osmel says:

    Great tip, huge help! Thanks a lot

  17. bluewater says:

    awesome, thx!

  18. Guest says:

    This code seems to work in multiple situations:

    protected override void OnHandleCreated(EventArgs e)
    if (RunTime())
    MessageBox.Show(“RunTime”, (this.Name + ” ” + this.GetType().Name).Trim());
    MessageBox.Show(“DesignTime”, (this.Name + ” ” + this.GetType().Name).Trim());

    private bool RunTime()
    Form parentForm = this.FindForm();
    Control parentControl = this.Parent;
    // Start with highest level and work down.
    if (parentForm != null)
    if (parentForm.Site != null && parentForm.Site.DesignMode)
    return false;
    return true;
    else if (parentControl != null)
    if (parentControl.Site != null && parentControl.Site.DesignMode)
    return false;
    return true;
    else if (this.Site != null && this.Site.DesignMode)
    return false;
    return true;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: