Winforms 扩展控件属性
我正在扩展 Winforms Label 控件。 (自定义标签)。定义如下:
public class CustomLabel: Label
public CustomLabel():base()
{
}
我想更改标签的默认文本。它始终是 CustomLabel1
、CustomLabel2
等。
我在构造函数中尝试了 base.Text = ...
和 this.Text=...
。也尝试过:
[DefaultValue(typeof(string), "MyDesiredText")]
public override string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
}
}
但没有运气。
还有一件事:Autosize 属性似乎没有按预期工作,并且提供的空间比第一次查看自定义标签控件所需的空间少 1 个字符。例如,默认文本为:CustomLabel1
,但当我将控件拖动到窗体时,我只看到 CustomLabel
。如果我更改文本,“自动调整大小”属性将正确显示整个文本。
I'm extending a Winforms Label control. (CustomLabel). Here's the definition:
public class CustomLabel: Label
public CustomLabel():base()
{
}
I'd like to change the label's default text. it's always CustomLabel1
, CustomLabel2
, etc.
I tried base.Text = ...
and this.Text=...
in the constructor. Also tried:
[DefaultValue(typeof(string), "MyDesiredText")]
public override string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
}
}
But no luck.
And one more thing: It seems like Autosize property doesn't work as expected and provides 1 character less space than necessary to view the custom label control for the first time. For example, the default text is: CustomLabel1
but I only see CustomLabel
when I drag the control to the form. If I change the text, the Autosize property will show the whole text correctly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
真正的修复需要更换控件的设计者。这很难做到,System.Design.dll 中的 LabelDesigner 类是内部的,因此您无法继承它。黑客的方法是这样的:
A real fix requires replacing the designer for the control. That's quite hard to do, the LabelDesigner class in System.Design.dll is internal so you can't inherit it. The hacky way is this: