I'd like to change the file name of the SaveFileDialog
in the event handler attached to the FileOk
event, in order to replace the file name typed in by the user with another file name in some cases, while keeping the dialog open:
var dialog = new SaveFileDialog();
...
dialog.FileOk +=
delegate (object sender, CancelEventArgs e)
{
if (dialog.FileName.EndsWith (".foo"))
{
dialog.FileName = "xyz.bar";
e.Cancel = true;
}
};
Stepping through the code shows that the FileName
gets indeed properly updated, but when the event handler returns, the file name displayed in the dialog does not change. I've seen that I could theoretically use Win32 code like the following to change the file name in the dialog itself:
class Win32
{
[DllImport("User32")]
public static extern IntPtr GetParent(IntPtr);
[DllImport("User32")]
public static extern int SetDlgItemText(IntPtr, int string, int);
public const int FileTitleCntrlID = 0x47c;
}
void SetFileName(IntPtr hdlg, string name)
{
Win32.SetDlgItemText (Win32.GetParent (hdlg), Win32.FileTitleCntrlID, name);
}
However, I've no idea where I can get the HDLG
associated to the SaveFileDialog
instance from. I know I can rewrite the whole SaveFileDialog
wrapper myself (or use code like NuffSaveFileDialog or the CodeProject extension of SaveFileDialog), but I'd prefer to use the standard WinForms classes for technical reasons.