开发者

.NET 2010 custom control, multiline String property to be edited in the designer

I'm writing a custom control and I want to add a "MessageText" property of type String:

<Browsable(True),
  DefaultValue(""),
  Category("CustomControls"),
  Description("Blah."),
  DesignerSerializationVisibility(DesignerSerializationVisibil开发者_开发问答ity.Visible)>
  Public Property MessageText As String

The MessageText property is a multiline text, and the user must be able to set the text using the designer. The problem is that the designer doesn't allow to enter a newline directly for a string property.

I want the same behaviour as the system TextBox's Text property, where you can click on the down arrow and write lines in the small text-editor that appears:

.NET 2010 custom control, multiline String property to be edited in the designer

How do I do that?


This is the declaration for the TextBoxBase.Text property:

[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..  
}

The [Editor] attribute is what you need. There is hassle if you also use .NET 4.0 (note the version string). It is better to use the alternative version of the constructor. Project + Add Reference, select System.Design. Make it look like this:

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... 
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜