tags:

views:

132

answers:

3

I have a property grid control where I want to be able to display a SaveFileDialog as the user is in the process of exporting data to a new file. I can easily hook up an OpenFileDialog with a FileNameEditor but there doesn't seem to be an equivalent class for saving files.

Is there an existing class that I can specify in the System.ComponentModel.Editor attribute so that a SaveFileDialog is displayed?

A: 

I don't think there is. You will have to write your own editor derived from UITypeEditor. It should not be that difficult.

logicnp
+1  A: 

This works fine:

public class SaveFileNameEditor: UITypeEditor
{
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        return UITypeEditorEditStyle.Modal;
    }

    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        if (context == null || context.Instance == null || provider == null)
        {
            return base.EditValue(context, provider, value);
        }

        using (SaveFileDialog saveFileDialog = new SaveFileDialog())
        {
            if (value != null)
            {
                saveFileDialog.FileName = value.ToString();
            }

            saveFileDialog.Title = context.PropertyDescriptor.DisplayName;
            saveFileDialog.Filter = "All files (*.*)|*.*";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                value = saveFileDialog.FileName;
            }
        }

        return value;
    }
}
Stewy
A: 

So the object that you set in the propertyGrid1.SelectedObject needs a public property like the following:

            private string _saveFile;
 [BrowsableAttribute(true)]
 [EditorAttribute(typeof(SaveFileNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
 public string SaveFileEditorVlad
 {
  get { return _saveFile; }
  set { _saveFile = value; }
 }

in order to make Stewy's answer work :) Then at runtime, when you edit the this property, the ellipsis will show and you'll be able to select a file to Save As.

tzup