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

旧城冷巷雨未停 提交于 2019-12-25 04:43:09

问题


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(DesignerSerializationVisibility.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:

How do I do that?


回答1:


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


来源:https://stackoverflow.com/questions/3759487/net-2010-custom-control-multiline-string-property-to-be-edited-in-the-designer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!