Is there a better StringCollection editor to use in PropertyGrids?

I use PropertySheets heavily in my application infrastructure configuration editor. I really like them because it’s very easy to work with them (as soon as you know) and make editing bulletproof.

One of the things I save in my configuration is Python scripts. You can edit the Python script in the StringCollection editor, which I use, but there is a long distance between them "maybe" and "useful." I would like to have an editor that actually supported resizable and monospaced fonts, kept blank lines and, hey, let go of the wish list - made syntax coloring.

I can of course write this if I really need to, but I would prefer not to.

I poked at Google and cannot find anything similar to what I am describing, so I thought I would ask here. Is this a solvable problem? Has anyone already hacked while creating a better editor?

+4
source share
2 answers

You can easily create your own line editor by following these simple steps. This example uses C #.

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

 public class StringArrayEditor : System.Drawing.Design.UITypeEditor 

The PropertyGrid control needs to know that the editor is modal, and it displays the ellipses button when the selected property is selected. Therefore, you should override GetEditStyle as follows:

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

Finally, the editor control must override the EditValue operation EditValue that it knows how you want to continue when the user clicks the ellipse button for your property. Here is the complete code to 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's going on? When a user clicks on ellipses, this override is called. editorService installed as an interface for our editing form. It is set to a form that we have not created yet, which I call TextArrayPropertyForm . TextArrayPropertyForm is created by passing an editable value. For good measure, I also pass 2 lines, one for the name of the form, and the other for the label at the top, explaining what the user should do. It is shown modally, and if the "OK" button is pressed, the value is updated regardless of what 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 an editor form. In my case, I created a form with 2 buttons ( buttonOK and buttonCancel ) label ( labelInstructions ) and text box ( 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 an alternative editor. Changing 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 in the form and set the class containing this Text property, it will edit in your custom form. There are countless possibilities for modifying your custom form of your choice. With changes, this will work for editing any type you like. The important thing is that the editor control returns the same type as the property in the overridden EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)

Hope this helps!

+3
source

You will need to write your own type editor. You can think of it as a user control, because when you write your own type editor, you provide user interface controls that appear when the property grid changes the property. That way, you can create a type editor that does nothing, which means that if you have a third-party editor, you can enable it as part of the type editor.

Some resources to get started:

+2
source

All Articles