编程语言
首页 > 编程语言> > c# – 是否有更好的StringCollection编辑器可以在PropertyGrids中使用?

c# – 是否有更好的StringCollection编辑器可以在PropertyGrids中使用?

作者:互联网

我在应用程序框架的配置编辑器中大量使用PropertySheets.我非常喜欢它们,因为它很容易与它们一起工作(一旦你学会了)并使编辑变得无懈可击.

我在配置中存储的一件事是Python脚本.可以在StringCollection编辑器中编辑Python脚本,这是我一直在使用的,但“可能”和“可用”之间有很长的距离.我想要一个实际上支持可调整大小和等宽字体的编辑器,保留空白行,并且 – 嘿,让我们对愿望清单疯狂 – 做语法着色.

如果我真的需要,我当然可以写这个,但我不愿意.

我在谷歌上搜索过,找不到我所描述的内容,所以我想我会在这里问.这是一个解决的问题吗?有没有人已经在建立一个更好的编辑器?

解决方法:

您可以按照以下简单步骤轻松创建自己的字符串集合编辑器.此示例使用C#.

1)您必须创建一个编辑器控件并从System.Drawing.Design.UITypeEditor派生它.我打电话给我的StringArrayEditor.因此,我的班级开始

public class StringArrayEditor : System.Drawing.Design.UITypeEditor

PropertyGrid控件需要知道编辑器是模态的,并且在选择有问题的属性时它将显示省略号按钮.所以你必须覆盖GetEditStyle,如下所示:

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        return UITypeEditorEditStyle.Modal;
    }

最后,编辑器控件必须覆盖EditValue操作,以便在用户单击属性的省略号按钮时知道如何继续.以下是覆盖的完整代码:

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        var editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
        if (editorService != null)
        {
            var selectionControl = new TextArrayPropertyForm((string[])value, "Edit the lines of text", "Label Editor");
            editorService.ShowDialog(selectionControl);
            if (selectionControl.DialogResult == DialogResult.OK)
                value = selectionControl.Value;
        }
        return value ?? new string[] {};
    }

那么发生了什么?当用户单击省略号时,将调用此覆盖. editorService被设置为编辑表单的界面.它被设置为我们尚未创建的形式,我称之为TextArrayPropertyForm. TextArrayPropertyForm被实例化,传递要编辑的值.为了更好的衡量,我也传递了两个字符串,一个用于表单标题,另一个用于顶部的标签,用于解释用户应该做什么.它以模态显示,如果单击了OK按钮,则使用我们将创建的表单中的selectionControl.Value中设置的值更新值.最后,在覆盖结束时返回此值.

步骤2)创建编辑器表单.在我的例子中,我创建了一个带有2个按钮(buttonOK和buttonCancel)的表单(LabelInstructions)和一个TextBox(textValue)来模仿默认的StringCollection编辑器.代码很简单,但是如果你感兴趣的话,就在这里.

using System;
using System.Windows.Forms;

namespace MyNamespace
{
    /// <summary>
    /// Alternate form for editing string arrays in PropertyGrid control
    /// </summary>
    public partial class TextArrayPropertyForm : Form
    {
        public TextArrayPropertyForm(string[] value,
            string instructions = "Enter the strings in the collection (one per line):", string title = "String Collection Editor")
        {
            InitializeComponent();
            Value = value;
            textValue.Text = string.Join("\r\n", value);
            labelInstructions.Text = instructions;
            Text = title;
        }

        public string[] Value;

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            Value = textValue.Text.Split(new[] { "\r\n" }, StringSplitOptions.None);
            DialogResult = DialogResult.OK;
        }
    }
}

步骤3)告诉PropertyGrid使用备用编辑器.此属性与PropertyGrid控件中使用的任何其他属性之间的更改是[Editor]行.

    [Description("The name or text to appear on the layout.")]
    [DisplayName("Text"), Browsable(true), Category("Design")]
    [Editor(typeof(StringArrayEditor), typeof(System.Drawing.Design.UITypeEditor))]
    public string[] Text {get; set;}

现在,当您在窗体上创建一个PropertyGrid并设置为包含此Text属性的类时,它将在您自定义的窗体中进行编辑.有无数的机会以您选择的方式更改您的自定义表单.通过修改,这将适用于编辑您喜欢的任何类型.重要的是编辑器控件返回的类型与重写的EditValue中的属性相同(ITypeDescriptorContext上下文,IServiceProvider提供程序,对象值)

希望这可以帮助!

标签:python,c,net,editor,propertygrid
来源: https://codeday.me/bug/20190622/1260875.html