是否有更好的用于 PropertyGrid 的 StringCollection 编辑器?

发布于 2024-07-08 19:55:07 字数 383 浏览 5 评论 0原文

我在应用程序框架的配置编辑器中大量使用 PropertySheets。 我非常喜欢它们,因为使用它们非常容易(一旦你学会了如何使用)并且使编辑变得万无一失。

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

如果确实有必要的话,我当然可以写这篇文章,但我宁愿不写。

我在谷歌上查了一下,找不到像我所描述的那样的东西,所以我想我应该在这里问。 这是一个已解决的问题吗? 有没有人已经尝试过构建一个更好的编辑器?

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.

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夕嗳→ 2024-07-15 19:55:07

按照以下简单步骤,您可以轻松创建自己的字符串集合编辑器。 此示例使用 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 设置为我们编辑表单的接口。 它被设置为我们尚未创建的表单,我称之为 TextArrayPropertyFormTextArrayPropertyForm 被实例化,传递要编辑的值。 为了更好地衡量,我还传递了 2 个字符串,一个用于表单标题,另一个用于顶部的标签,解释用户应该做什么。 它以模态方式显示,如果单击“确定”按钮,则该值将使用我们将创建的表单中 selectionControl.Value 中设置的值进行更新。 最后,该值在覆盖结束时返回。

步骤2)创建编辑器表单。 就我而言,我创建了一个带有 2 个按钮(buttonOKbuttonCancel)、一个标签 (labelInstructions) 和一个文本框 (textValue< /code>) 来模仿默认的 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 context, IServiceProviderprovider, object value) 中的属性相同的类型

希望这有帮助!

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

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

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;
    }

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[] {};
    }

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.

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;
        }
    }
}

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 [Editor] 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;}

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)

Hope this helps!

撑一把青伞 2024-07-15 19:55:07

您需要编写自己的类型编辑器。 您可以将其视为用户控件,因为当您编写自己的类型编辑器时,您将提供在属性网格编辑属性时出现的 UI 控件。 因此,您可以创建一个执行任何操作的类型编辑器,这意味着如果您有第三方编辑器控件,您可以将其包含为类型编辑器的一部分。

一些可帮助您入门的资源:

You would need to write your own type editor. You can think of this as a user control, in that when you write your own type editor you are providing the UI controls that appear when the property grid edits the property. As such, you can create a type editor that does anything, which means if you have a third-party editor control you can include it as part of type editor.

Some resources to get you started:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文