.NET 2010自定义控件,要在设计器中编辑的多行String属性

时间:2010-09-21 10:40:58

标签: vb.net custom-controls

我正在编写自定义控件,我想添加一个String类型的“MessageText”属性:

<Browsable(True),
  DefaultValue(""),
  Category("CustomControls"),
  Description("Blah."),
  DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)>
  Public Property MessageText As String

MessageText属性是一个多行文本,用户必须能够使用设计器设置文本。问题是设计者不允许直接为字符串属性输入换行符。

我想要与系统TextBox的Text属性相同的行为,您可以在其中单击向下箭头并在出现的小文本编辑器中写入行:

我该怎么做?

1 个答案:

答案 0 :(得分:3)

这是TextBoxBase.Text属性的声明:

[Editor("System.ComponentModel.Design.MultilineStringEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor)), Localizable(true)]
public override string Text
{
    // etc..  
}

[Editor]属性是您需要的。如果您还使用.NET 4.0(请注意版本字符串)会有麻烦。最好使用构造函数的替代版本。 Project + Add Reference,选择System.Design。看起来像这样:

using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
...
        [Editor(typeof(System.ComponentModel.Design.MultilineStringEditor), typeof(UITypeEditor))]
        public string MessageText {
            // etc... 
        }