35

Is there a built-in editor for a multi-line string in a PropertyGrid.

fryguybob
  • 4,390
  • 2
  • 28
  • 36

4 Answers4

57

I found that System.Design.dll has System.ComponentModel.Design.MultilineStringEditor which can be used as follows:

public class Stuff
{
    [Editor(typeof(MultilineStringEditor), typeof(UITypeEditor))]
    public string MultiLineProperty { get; set; }
}
fryguybob
  • 4,390
  • 2
  • 28
  • 36
  • http://msdn.microsoft.com/en-us/library/system.componentmodel.design.multilinestringeditor.aspx indicates that it is in 2.0, 3.0, and 3.5. – fryguybob Sep 25 '08 at 20:08
  • 1
    How to use this? I am trying to write [Editor(typeof(MultilineStringEditor), typeof(UITypeEditor))] [CategoryAttribute("Misc"), Description("All http headers for this mail."), DisplayName("HTTP Headers")] public string HttpHeaders { get { return mail.HttpHeaders; } } but this does not display HttpHeaders as multiline. – Alex Jul 30 '10 at 18:50
  • 1
    It is a multiline *editor* so I think you would only see a difference when editing, so it would only apply to properties with a public setter. – fryguybob Jul 31 '10 at 04:31
  • Thanks fryguybob! Do you know a way to SHOW multiline string in a grid? Not to edit? Thanks! – Alex Aug 02 '10 at 14:10
  • UITypeEditor is in the namespace System.Drawing.Design – Matt Nelson Sep 05 '12 at 15:38
  • 2
    I can't find the MultilineStringEditor in the System.Design.dll of .Net Framework 4.6.1. Has it been removed or am I just blind? – Mats Oct 26 '16 at 08:17
  • To use this you'll need to add the `System.Design.dll` as a reference to your project. – Michael Minton Apr 03 '17 at 20:54
  • How to do this the propertygrid of the wpf extended lib – Amen Jlili Oct 21 '17 at 01:38
2

No, you will need to create what's called a modal UI type editor. You'll need to create a class that inherits from UITypeEditor. This is basically a form that gets shown when you click on the ellipsis button on the right side of the property you are editing.

The only drawback I found, was that I needed to decorate the specific string property with a specific attribute. It's been a while since I had to do that. I got this information from a book by Chris Sells called "Windows Forms Programming in C#"

There's a commercial propertygrid called Smart PropertyGrid.NET by VisualHint.

Hector Sosa Jr
  • 4,230
  • 27
  • 30
2

We need to write our custom editor to get the multiline support in property grid.

Here is the customer text editor class implemented from UITypeEditor

public class MultiLineTextEditor : UITypeEditor
{
    private IWindowsFormsEditorService _editorService;

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

    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        _editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

        TextBox textEditorBox = new TextBox();
        textEditorBox.Multiline = true;
        textEditorBox.ScrollBars = ScrollBars.Vertical;
        textEditorBox.Width = 250;
        textEditorBox.Height = 150;
        textEditorBox.BorderStyle = BorderStyle.None;
        textEditorBox.AcceptsReturn = true;
        textEditorBox.Text = value as string;

        _editorService.DropDownControl(textEditorBox);

        return textEditorBox.Text;
    }
}

Write your custom property grid and apply this Editor attribute to the property

class CustomPropertyGrid
{
    private string multiLineStr = string.Empty;

    [Editor(typeof(MultiLineTextEditor), typeof(UITypeEditor))]
    public string MultiLineStr
    {
        get { return multiLineStr; }
        set { multiLineStr = value; }
    }
}

In main form assign this object

 propertyGrid1.SelectedObject = new CustomPropertyGrid();
Jimi
  • 29,621
  • 8
  • 43
  • 61
Sunil Kumar
  • 111
  • 1
  • 5
0

Yes. I don't quite remember how it is called, but look at the Items property editor for something like ComboBox

Edited: As of @fryguybob, ComboBox.Items uses the System.Windows.Forms.Design.ListControlStringCollectionEditor

Ilya Ryzhenkov
  • 11,782
  • 1
  • 40
  • 50
  • ComboBox.Items uses the System.Windows.Forms.Design.ListControlStringCollectionEditor, I don't think that is quite what I'm looking for, but it is close. – fryguybob Sep 24 '08 at 21:31