Winform Custom Control: DesignMode doesn't return true whereas in Design Mode
Asked Answered
A

4

25

I learnt about DesignMode here How to refresh a winform custom control at design time after changing a property

But when in the constructor of my custom control I use it, it never returns true so when I drag and drop my custom control it always show max = 200 so what's wrong?

if (this.DesignMode)
{                
    this.Min = 0;
    this.Max = 100;
} 
else 
{
    this.Min = 0;
    this.Max = 200;            
}

this.LabMin.Text = this.Min.ToString();
this.LabMax.Text = this.Max.ToString();  
Adlare answered 3/12, 2010 at 14:13 Comment(2)
Any particular reason why you want the max value to be higher at run-time than at design time?Bering
No it's just to test designmode 2 different value :)Adlare
T
47

LicenseManager.UsageMode is intended for this.

It is in fact the only reliable way to detect if your control is in design mode or not. It's only valid during the constructor, but it can easily be stored in a field of the class for later reference.

The DesignMode property for nested controls will be false even when the container control is in design mode.

Taryn answered 3/12, 2010 at 15:16 Comment(6)
Thanks for suggestion, I'll try but this is really ugly from Microsof I'm very upset, giving a name that doesn't do what its name intends to is a shame.Adlare
What a completely useless field. Thanks Microsoft jeez.Albanese
@Adlare the name does what it says, but you need to care of when you call it. The documentation says it clearly.Cardio
@TobiasKnauss I'm not sure I follow. Your linked answer is entirely consistent with my answer: DesignMode is unreliable in nested controls while LicenseManager always gives the correct value in a constructor, and, if you want to check its value outside of the contructor, you should persist it in a field during the constructor.Taryn
You're right, sorry. Looks like I posted my comment on the answer of the wrong post. There are multiple questions on this topic, but it seems that you provided the only correct answer. I deleted my comments.Larrisa
I read this as caching the value in the constructor. However, defining a field like the following seems to works as well. The field doesn't have to be set in the constructor itself: private LicenseUsageMode mUsageMode = LicenseManager.UsageMode;Chubby
M
5

It could be that the control must also must have an ISite associated with it, otherwise it will always return false

The design mode indicator is stored in the ISite; therefore, if the Component does not have an ISite associated with it, the value of this property is false.

Source: MSDN

Edit: Also see this post as someone had a similar problem to the one you're facing

Windows Forms designer and DesignMode property issues

Edit 2: I also found a site that seems to indicate that this is a common problem with custom controls but it also lists some work arounds. You can find it here:

Custom Control Design Mode Problem

Motherinlaw answered 3/12, 2010 at 14:21 Comment(1)
Wow thanks but what an ugly plumbing mess just to make something simple in intent sometimes I really hate Microsoft :)Adlare
A
3

For what I remember in the Ctor the DesignMode property has not its value yet. You should use it after initializeComponents or in an event handler.

Arequipa answered 3/12, 2010 at 14:17 Comment(1)
+1 This is mostly correct: the DesignMode property will always return "False" in a constructor method. However, a call to the InitializeComponents method cannot be counted on to reliably change this behavior. You need to use it in an event handler (as suggested), a property, or another method.Bering
E
0

For any that arrive at this topic, there's another way to handle this.

If you implement ISupportInitialize for your winforms control, the designer code will call begin/end initialize of your control.

then, for all of your design time and runtime changes, you can adjust in the EndInitialization method. by that time, the Site.DesignMode property will have been set to True/False.

I also like to add a class level boolean, i.e. bool _initializing = false; so i can track that state in other places.

this seems to be the least "hacky" way of handling such stuff.

Ensheathe answered 8/8, 2019 at 17:37 Comment(1)
In addition, in the designer code for parent control/form, the "Add" of the child control to the container happens after all the layout operations of child controls. If you check for this.Parent == null, you can use this to determine some state of things. If there's no parent yet assigned for your control, there's a chance that DesginMode might not have been changed yet.Ensheathe

© 2022 - 2024 — McMap. All rights reserved.