jAC
jAC

Reputation: 3435

How to do something if cancel button on save file dialog was clicked?

I am using c# WinForms. I have a save dialog box that pops up and a message box after that that says it was saved successfully.

I just realized that if a user clicks cancel, my message box still comes.

How do i tell when a user clicks the cancel button on a save dialog box and then do something when it is cancelled?

Upvotes: 7

Views: 21365

Answers (3)

Lucas Garcia
Lucas Garcia

Reputation: 1

In case you are using WPF in a open file dialog, the better option I found is to store the selected file path (File.ReadAllText(filedialog.Filename) ) in a string and then check if its !== null wich means the user has a file selected. If the file path is null so the file is empty or the user just clicked on cancel button

Upvotes: 0

plinth
plinth

Reputation: 49189

A save dialog box after closing has the DialogResult property set to what happens. In your case:

if (mySaveDialog.DialogResult == DialogResult.OK) { /* show saved ok */ }

Upvotes: 13

Habib
Habib

Reputation: 223247

Use DialogResult

if (form.ShowDialog() == DialogResult.Cancel)
{
    //user cancelled out
}

For SaveFileDialog:

SaveFileDialog saveFileDialog = new SaveFileDialog();
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
    MessageBox.Show("your Message");
}

Upvotes: 17

Related Questions