有没有更好的StringCollection编辑在PropertyGrids用?编辑、StringCollection、PropertyGrids

2023-09-03 17:34:23 作者:最远的距离

我大量使用PropertySheets在我的应用程序框架的配置编辑器。我喜欢他们了很多,因为它是pretty的轻松与他们一起工作(一旦你学会了如何),并编辑防弹。

I'm making heavy use of PropertySheets in my application framework's configuration editor. I like them a lot because it's pretty easy to work with them (once you learn how) and make the editing bulletproof.

一个是我存储在我的配置中的东西都是Python脚本。这是可能的编辑在StringCollection编辑器,这是我利用一直是个Python脚本,但有可能和之间的长途可用。我想有一个编辑器,实际支持调整大小和等宽字体,preserved空白行,以及 - 嘿,让我们疯狂的心愿 - 做语法着色

One of the things that I'm storing in my configuration are Python scripts. It's possible to edit a Python script in a StringCollection editor, which is what I've been using, but there's a long distance between "possible" and "useable." I'd like to have an editor that actually supported resizeable and monospace fonts, preserved blank lines, and - hey, let's go crazy with the wishlist - did syntax coloring.

我当然可以写,如果我真的有,但是我preFER没有。

I can certainly write this if I really have to, but I'd prefer not to.

我已经在谷歌戳周围,也找不到像我所描述任何东西,所以我想我会在这里问。这是一个解决问题吗?有没有人在那里已经采取了裂纹建设一个更好的编辑器?

I've poked around on the Google and can't find anything like what I'm describing, so I thought I'd ask here. Is this a solved problem? Has anyone out there already taken a crack at building a better editor?

推荐答案

您可以轻松地创建自己的字符串集合编辑器,下面这些简单的步骤。 此示例使用C#。的

You can create your own string collection editor easily, following these simple steps. This example uses C#.

1),您必须创建一个编辑器控制,并从 System.Drawing.Design.UITypeEditor 得到它。我打电话给我的 StringArrayEditor 。因此,我的课开始

1) You must create an editor control and derive it from System.Drawing.Design.UITypeEditor. I called mine StringArrayEditor. Thus my class starts with

public class StringArrayEditor : System.Drawing.Design.UITypeEditor

的PropertyGrid 控件需要知道编辑是形式上的,它会显示椭圆形按钮时,有关财产被选中。所以,你必须重写 GetEditStyle 如下:

The PropertyGrid control needs to know that the editor is modal and it will show the ellipses button when the property in question is selected. So you must override GetEditStyle as follows:

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

最后编辑控件必须重写的EditValue 操作,以便它知道要如何继续当用户点击省略号按钮,你的财产。这里是全code的覆盖:

Lastly the editor control must override the EditValue operation so that it knows how you want to proceed when the user clicks on the ellipses button for your property. Here is the full code for the override:

        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 被实例化,传递给编辑的价值。良好的措施,我也通过2串,一个窗体标题,并在上​​面解释什么用户应做其他的标签。结果表明模态,如果被点击确定按钮,则该值与任何值是在 selectionControl.Value 设定距离,我们将创建表单更新。最后,这个值是在覆盖的末尾返回。

So what is happening? When the user clicks on the ellipses, this override is called. editorService is set as the interface for our editing form. It is set to the form which we haven't yet created that I call TextArrayPropertyForm. TextArrayPropertyForm is instantiated, passing the value to be edited. For good measure I am also passing 2 strings, one for the form title and the other for a label at the top explaining what the user should do. It is shown modally and if the OK button was clicked then the value is updated with whatever the value was set in selectionControl.Value from the form we will create. Finally this value is returned at the end of the override.

步骤2)创建编辑器的形式。在我来说,我有2个按钮( buttonOK buttonCancel )的标签(创建的窗体labelInstructions )和一个文本框( textValue )来模拟默认StringCollection编辑器。在code是pretty的直接的,但如果你有兴趣,在这儿呢。

Step 2) Create the editor form. In my case I created a form with 2 Buttons (buttonOK and buttonCancel) a Label (labelInstructions) and a TextBox (textValue) to mimic the default StringCollection editor. The code is pretty straight-forward, but in case you're interested, here it is.

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控件任何其他之间的变化是行。

Step 3)Tell the PropertyGrid to use the alternate editor. The change between this property and any other that is used in the PropertyGrid control is the line.

    [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的提供者,对象的值)

Now when you create a PropertyGrid on a form and set to the class containing this Text property it will edit in your custom made form. There are countless opportunities to change your custom form in ways you choose. With modifications this will work for editing any type you like. The important thing is that the editor control returns the same type as is the property in the overridden EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)

希望这有助于!