In Windows Forms, I'm representing a custom class in a PropertyGid
control, which has various string properties as you can notice in the next image:
The problem is that I'm not totally satisfied with the current behavior for changing the value of the string properties. For those properties that expects a file or directory path, like "Target" or "Working Directory" properties, I would like to know if it could be possible and viable to implement a TypeConverter / Type Descriptor that would open a OpenFileDialog
when clicking in the down arrow at the right of the field in the property grid. That is, to select a file or folder through a OpenFileDialog
, instead of directly writing the path in the property grid, but still letting the option to directly write the path if I want to do so.
Maybe .NET Framework class library already provides the TypeConverter / TypeDescriptor that I'm requesting?. If not, is this possible to do?. And how to start doing so?.
Or any other idea to be able open a OpenFileDialog
to change the value of a specific property in a PropertyGrid
control?.
There are builtin FileNameEditor
and FolderNameEditor
UI type editors which let you choose file name and folder name, for example:
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
public class MyClass
{
[Editor(typeof(FileNameEditor), typeof(UITypeEditor))]
public string FilePath { get; set; }
[Editor(typeof(FolderNameEditor), typeof(UITypeEditor))]
public string FolderPath { get; set; }
}
If you want to customize the FileNameEditor
to show just txt files, you can override its InitializeDialog
method:
public class MyFileNameEditor : FileNameEditor
{
protected override void InitializeDialog(OpenFileDialog openFileDialog)
{
base.InitializeDialog(openFileDialog);
openFileDialog.Filter = "text files (*.txt)|*.txt";
}
}