在"属性"窗口中允许多行字符串属性

Dav*_*vid 9 c# visual-studio-2008 windows-forms-designer

我有一个带有字符串属性的Windows窗体用户控件,用于设置文本框的文本.这个字符串可以是多行的.

我注意到在一些带有文本属性的控件上,而不是强制键入单行属性文本框,你可以弹出一个可以键入多行的地方.(事实上​​,Windows窗体文本框控件允许在Text属性上使用它.)

如何在属性窗口中为我设计的属性启用此功能?

以下不是我的应用程序中的真实代码,而是如何定义此类属性的示例

public string Instructions
{
   get
   {
      return TextBox1.Text;
   }
   set
   {
      TextBox1.Text = value;
   }
}
Run Code Online (Sandbox Code Playgroud)

man*_*nji 16

您可以使用EditorAttribute一个MultilineStringEditor:

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

为了避免添加对System.Design的引用并因此需要Full框架,您还可以编写如下属性:

[EditorAttribute(
    "System.ComponentModel.Design.MultilineStringEditor, System.Design",
    "System.Drawing.Design.UITypeEditor")]
Run Code Online (Sandbox Code Playgroud)

虽然现在他们已经停止将框架拆分为客户端配置文件和完整客户端配置文件,但这不是一个问题.

  • 添加对System.Design的引用要求目标框架不是Client Profile而是Full.作为替代方案,编写如下属性:`[EditorAttribute("System.ComponentModel.Design.MultilineStringEditor,System.Design","System.Drawing.Design.UITypeEditor")]` - 这也适用于客户端配置文件! (3认同)
  • 你必须引用`System.Design.dll` (2认同)