C# – How to do something if cancel button on save file dialog was clicked

csavefiledialogwinforms

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?

Best Answer

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