views:

202

answers:

2

Hi All,

I have a FileNameEditor inside a property grid, which has a few entries like

Main File : "C:\blah1"

Sec File: "C:\blah2"

and so on.

My problem is that I cannot copy and paste from one property entry to another, and I cannot type in the fields manually as well. Is there a specific property that will enable editing inside the FileNameEditor. Example

public class MyEditor : FileNameEditor
{
    public override bool GetPaintValueSupported(ITypeDescriptorContext context)
    {
        return false;
    }

    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)

    {

        object e = base.EditValue(context, provider, value);

        if ((value as MyFileS) != null)
        {
            (value as MyFilesS).FileName = (String) e;
        }

        return e;
    }

    protected override void InitializeDialog(OpenFileDialog openFileDialog)
    {
        base.InitializeDialog(openFileDialog);
    }
}

Thanks

+1  A: 

Cannot reproduce; this works fine - can copy/paste both in the grid and the popup (does your property have a setter?):

using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
class Foo
{
    [Editor(typeof(FileNameEditor), typeof(UITypeEditor))]
    public string Name { get; set; }

    [STAThread]
    static void Main()
    {
        using (Form form = new Form())
        using (PropertyGrid grid = new PropertyGrid())
        {
            grid.Dock = DockStyle.Fill;
            form.Controls.Add(grid);
            grid.SelectedObject = new Foo();
            Application.Run(form);
        }
    }
}
Marc Gravell
+1  A: 

Why are you using a custom editor? If you just want a string property on an object then Marc Gravell's answer works.

However if the "File" property on the object within the property grid is a custom class you also need to implement a custom type converter.

Eg:

namespace WindowsFormsApplication
{
    using System;
    using System.ComponentModel;
    using System.Drawing.Design;
    using System.Globalization;
    using System.Windows.Forms;
    using System.Windows.Forms.Design;

    class MyEditor : FileNameEditor
    {
        public override bool GetPaintValueSupported(ITypeDescriptorContext context)
        {
            return false;
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            string s = Environment.CurrentDirectory;
            object e = base.EditValue(context, provider, value);
            Environment.CurrentDirectory = s;

            var myFile = value as MyFile;
            if (myFile != null && e is string)
            {
                myFile.FileName = (string)e;
                return myFile;
            }

            return e;
        }
    }

    class MyFileTypeConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
                return true;

            return base.CanConvertFrom(context, sourceType);
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(string))
                return true;

            return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string)
                return new MyFile { FileName = (string)value };

            return base.ConvertFrom(context, culture, value);
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            var myFile = value as MyFile;
            if (myFile != null && destinationType == typeof(string))
                return myFile.FileName;

            return base.ConvertTo(context, culture, value, destinationType);
        }
    }

    [TypeConverter(typeof(MyFileTypeConverter))]
    [Editor(typeof(MyEditor), typeof(UITypeEditor))]
    class MyFile
    {
        public string FileName { get; set; }
    }

    class MyFileContainer
    {
        public MyFileContainer()
        {
            File1 = new MyFile { FileName = "myFile1.txt" };
            File2 = new MyFile { FileName = "myFile2.txt" };
        }

        public MyFile File1 { get; set; }
        public MyFile File2 { get; set; }
    }

    static public class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            using (var f = new Form())
            using (var pg = new PropertyGrid())
            {
                pg.Dock = DockStyle.Fill;
                pg.SelectedObject = new MyFileContainer();
                f.Controls.Add(pg);
                Application.Run(f);
            }
        }
    }
}

To support editing the filename in place and also cut and paste the PropertyGrid needs to know how to convert from a string to your "File" type and back again. If you do not implement the convert to string methods in the TypeConverter the property will display the result of the object's ToString().

I suggest whipping out Reflector.Net and reading the source to some of the UITypeEditors and TypeConverters in the BCL as it can be quite informative to see how Microsoft supports editing Color, TimeSpan, DateTime etc in property grids.

Also, be careful opening file dialogs. The standard WinForms open file dialog can change your applications current working directory if you're not careful. I don't think FileNameEditor has this problem, but I've only tested this under Windows 7.

orj