Control.DesignMode Misbehaving?

Thursday, August 25th, 2005

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:
LicenseManager.UsageMode == LicenseUsageMode.Designtime

There you have it.

Tags: , ,

2 Responses to “Control.DesignMode Misbehaving?”

  1. Mike says:

    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. Mike says:

    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

Logo
Post thumbnail Video: LASERS!
Who doesn’t love lasers? Favourite tool of the James Bond villian, and weapon of choice [...]
Post thumbnail Video: Smartphone Roundup
With the recent launch of Apple’s iPhone 4, it’s time to do a once-over of [...]
Post thumbnail Video: Gadgets for Grandparents
Grandparents and gadgets don’t really go together, but this week on TVNZ Breakfast I took [...]
Post thumbnail Review: Kick Ass
I don’t usually review movies, but after giving two copies of Kick Ass away [...]
Post thumbnail Video: LASERS!
Who doesn’t love lasers? Favourite tool of the James Bond villian, and weapon of choice [...]
Post thumbnail State of the Android NZ Nation
Despite the bagging, I really do think Android is pretty damn awesome. I’ve spent the [...]

Enter your email address:


Delivered by FeedBurner