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
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:
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.