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 {
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!