FolderNameEditor, as in:
[Editor(typeof(FolderNameEditor), typeof(UITypeEditor))]
However, I notice that it loses the existing value when you hit the
[...] (i.e. it doesn't start at the current folder). I hastily cobbled
the following together, which does that, and also uses the Description
etc to set a suitable caption...
Marc
public class Foo
{
[Description("Where does it go?")]
[Editor(typeof(MyFolderNameEditor), typeof(UITypeEditor))]
public string Bar { get; set; }
}
public class MyFolderNameEditor : UITypeEditor
{
public override UITypeEditorEditStyle
GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
public override object EditValue(ITypeDescriptorContext context,
IServiceProvider provider, object value)
{
string path = Convert.ToString(value);
using (FolderBrowserDialog dlg = new FolderBrowserDialog())
{
if (!string.IsNullOrEmpty(path) && Directory.Exists(path))
{
dlg.SelectedPath = path;
}
if (context != null && context.PropertyDescriptor != null)
{
// pick [Description], [DisplayName] or Name in that
order
string caption =
context.PropertyDescriptor.Description;
if (string.IsNullOrEmpty(caption))
{
caption = context.PropertyDescriptor.DisplayName;
}
if (string.IsNullOrEmpty(caption))
{
caption = context.PropertyDescriptor.Name;
}
dlg.Description = caption;
}
if (dlg.ShowDialog() == DialogResult.OK)
{
path = dlg.SelectedPath;
}
}
return path;
}
}