Control.DesignMode Misbehaving?

The point of DesignMode is to detect when a .NET control is being designed, versus when the control is being loaded in runtime. This is useful for example when you have a control that retrieves data from a database: you don’t want that control accessing the database when you’re simply moving stuff around in the IDE. The problem is that DesignMode is quite literal in its behaviour. If for example your control is nested on another control, and that control is dragged onto a form, your control will not report that it is in DesignMode. You’ll also find that DesignMode doesn’t work in your control’s constructor either, because it doesn’t yet know if it is in that mode (or more correctly the ISite property has not been initialised).

Searching around the internets, I’ve found a (so far) infallible way of detecting if your control is being loaded in some form of design mode:
[code]LicenseManager.UsageMode == LicenseUsageMode.Designtime

There you have it.

2 Replies to “Control.DesignMode Misbehaving?”

  1. Hi there,

    One solution to the DesignMode problem that I have found that seems to work is to override the OnCreateControl method. Place anything that you would have placed into your constructor into this method, after the base implementation and wrap it in an if(!DesignMode). At this point, the control seems to be able to determine if it is in design mode or not. I don’t know if it will work for every eventuality, but it seems worth a try for now. I have made a note of your suggestion though! Just in case.

    Regards,

    Mike

  2. Hi there,

    One solution to the DesignMode problem that I have found that seems to work is to override the OnCreateControl method. Place anything that you would have placed into your constructor into this method, after the base implementation and wrap it in an if(!DesignMode). At this point, the control seems to be able to determine if it is in design mode or not. I don’t know if it will work for every eventuality, but it seems worth a try for now. I have made a note of your suggestion though! Just in case.

    Regards,

    Mike

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.