向 .NET 控件添加新属性

Sha*_*500 1 c# winforms c#-3.0

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

类似于 IsWordWrap(new custom property)=true。(这样的话可以自动换行)

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

文本框具有自动换行属性,有什么办法可以继承它来标记吗?

Ed *_* S. 5

您将从派生一个新类Label并添加所需的逻辑。不过,简单地将 a 设置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.
    }
}
Run Code Online (Sandbox Code Playgroud)