nirav patel
nirav patel

Reputation: 465

WPF - SaveFileDialog

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

So what do I have to do to tackle it? Anything in WPF, something like 'DialogResult' that is the same as in winForms?

Upvotes: 18

Views: 40455

Answers (4)

Ahmad
Ahmad

Reputation: 782

It will Perform as you want on cancel and ok Button of SaveFileDialog

bool? DialougeResult = saveFileDialog1.ShowDialog();
    if(DialougeResult)
    {
    // your code now its cancel when click cancel button 
    }

Upvotes: 0

S2S2
S2S2

Reputation: 8512

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;
}

Upvotes: 53

You have to use DialogResult Property for using a dialog result in WPF. For more information on using dialogresult in WPF refer to WPF Dialogs and DialogResult

Upvotes: 1

Pankaj Upadhyay
Pankaj Upadhyay

Reputation: 13594

You need to make use of MessageBox in WPF to open another window when users click cancel. Add the following code to the cancel button event :-

private void canceButton()
    {
        MessageBoxResult key = MessageBox.Show(
            "Are you sure you want to quit",
            "Confirm",
            MessageBoxButton.YesNo,
            MessageBoxImage.Question,
            MessageBoxResult.No);
        if (key == MessageBoxResult.No)
        {
            return;
        }
        else
        {
            Application.Current.Shutdown();
        }
    }

Upvotes: 3

Related Questions