Allow multi-line String properties in the Properties window

无人久伴 提交于 2019-12-17 19:49:00

问题


I've got a Windows Form User Control with a string property for setting the text of a textbox. This string can be multi-line.

I've noticed that on some controls with a text property, and instead of being forced to type in the single line property textbox, you get a little pop up where you can type multiple lines. (As a matter of fact, a Windows Forms Textbox control allows this on the Text property.)

How do I enable this functionality in the properties window for the property I have designed?

The following is not real code in my app, but an example of how such a property might be defined

public string Instructions
{
   get
   {
      return TextBox1.Text;
   }
   set
   {
      TextBox1.Text = value;
   }
}

回答1:


You can use the EditorAttribute with a MultilineStringEditor:

[EditorAttribute(typeof(MultilineStringEditor), 
                 typeof(System.Drawing.Design.UITypeEditor))]  
public string Instructions
{
   get
   {
      return TextBox1.Text;
   }
   set
   {
      TextBox1.Text = value;
   }
}

To avoid adding a reference to System.Design and thus requiring the Full framework, you can also write the attribute like this:

[EditorAttribute(
    "System.ComponentModel.Design.MultilineStringEditor, System.Design",
    "System.Drawing.Design.UITypeEditor")]

Although this is less of a problem now that they've stopped splitting the framework into a Client Profile and a Full one.



来源:https://stackoverflow.com/questions/1711880/allow-multi-line-string-properties-in-the-properties-window

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