Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - SaveFileDialog

Tags:

c#

wpf

I am using the SaveFileDialog in WPF to export into excel file at particular loaction selected by user. But in between when SaveFileDailog is opened then user clilks on Cancel button on dialog then i am getting the another dialog that says "Do you want to save changes you made to 'Sheet1'?" and then "Export completed" instead of cancelling to export.

So what i have to do to tackle with it? Any thing in WPF something like 'DialogResult' that is same as in winForms?

like image 405
nirav patel Avatar asked Nov 16 '11 06:11

nirav patel


1 Answers

SaveFileDialog will return true if user saved (the ShowDialog method returns a nullable bool), and return false/null if user pressed cancel. Below is a sample MSDN code to get you started:

// Configure save file dialog box
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "Document"; // Default file name
dlg.DefaultExt = ".txt"; // Default file extension
dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension

// Show save file dialog box
Nullable<bool> result = dlg.ShowDialog();

// Process save file dialog box results
if (result == true)
{
    // Save document
    string filename = dlg.FileName;
}
like image 148
VSS Avatar answered Oct 26 '22 21:10

VSS