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

    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

  • http://none Ashutosh

    May all your wish come true!

    Ashutosh

  • AdamT

    Good piece, helped me, thanks

    Adam,

  • Carl Scarlett

    Mate, this is an awesome tip.

    Thank you so much!

  • http://www.imperasoft.be Jurgen

    Very good tip!!

  • http://www.paul.com Paul

    Great, helped me!

  • Ant

    There is no LicenseManager in Compact Framework (((

  • Allan

    Very good tip, thank you

  • AdrianB

    Very useful, thanks a lot!
    Regards,

    Adrian

  • cht

    helped me al lot, thanks.

    cht

  • http://www.global-webnet.net/BlogEngine BillKrat
  • Pingback: C#/VB.Net: Knowing what mode you’re currently executing in | Under My Hat

  • Pingback: In Depth: A definitive guide to .NET user control’s usage mode: DesignMode or UserMode | Under My Hat

  • thed

    Works like a charm. Thanks!

  • David Wendelken

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

    http://decav.com/blogs/andre/archive/2007/04/18/1078.aspx

  • http://www.undermyhat.org Abel Braaksma

    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.

    http://www.undermyhat.org/blog/2009/07/in-depth-a-definitive-guide-to-net-user-controls-usage-mode-designmode-or-usermode/

    – Abel –

  • Osmel

    Great tip, huge help! Thanks a lot

  • bluewater

    awesome, thx!

  • Guest

    This code seems to work in multiple situations:

    protected override void OnHandleCreated(EventArgs e)
    {
    base.OnHandleCreated(e);
    if (RunTime())
    MessageBox.Show(“RunTime”, (this.Name + ” ” + this.GetType().Name).Trim());
    else
    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;
    else
    return true;
    else if (parentControl != null)
    {
    if (parentControl.Site != null && parentControl.Site.DesignMode)
    return false;
    else
    return true;
    }
    else if (this.Site != null && this.Site.DesignMode)
    return false;
    else
    return true;
    }