Allow multi-line String properties in the Properti

2020-02-06 02:17发布

问题:

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.