本文介绍了Winform的自定义控制:将designMode不而在设计模式返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解的designMode这里的刷新后,在设计时一个WinForm自定义控件



但是,当我的自定义控件的构造我使用它,所以当我拖放我的自定义控制它始终显示最大值= 200这样有什么不对?

 如果(这一点永远不会返回true .DesignMode)
{
this.Min = 0;
this.Max = 100;
}
,否则
{
this.Min = 0;
this.Max = 200;
}

this.LabMin.Text = this.Min.ToString();
this.LabMax.Text = this.Max.ToString();


解决方案

的是用于此。



事实上,它是检测,如果你的控制是在设计模式与否的唯一可靠的方法。这只是在构造过程中有效,但它可以容易地被存储在类供以后参考的字段。即使在容器控件处于设计模式



的designMode 属性嵌套的控制将是错误的。


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();  
解决方案

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.

这篇关于Winform的自定义控制:将designMode不而在设计模式返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 20:21