gpt4 book ai didi

c# - 向 .NET 控件添加新属性

转载 作者:太空宇宙 更新时间:2023-11-03 20:27:42 24 4
gpt4 key购买 nike

我正在尝试向现有标签 .NET 控件添加一个新属性,例如 AutoSize(现有标签属性),

类似于 IsWordWrap(新自定义属性)=true。 (这样的话可以自动换行)

有什么想法吗?这样我就可以给 LabelName.IsWordWrap=true;

Textbox 有 wordwrap 属性,有什么方法可以继承它来标记吗?

最佳答案

您将从 Label 派生一个新类并添加您需要的逻辑。不过,简单地将 TextBox 样式设置为看起来像标签会容易得多。

using System.Windows.Forms;
// ...

class WrappingLabel : Label
{
private bool _isWordWrap
public bool IsWordWrap
{
get { return _isWordWrap; }
set
{
if( _isWordWrap != value )
{
_isWordWrap = value;
FormatText( value );
}
}
}

private void FormatText( bool wrapped )
{
// logic to wrap or un-wrap text goes here.
// you will need to call this when the text changes as well.
}
}

关于c# - 向 .NET 控件添加新属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9509403/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com