Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 755
jAC Avatar asked Sep 05 '14 20:09

jAC


2 Answers

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");
}
like image 149
Habib Avatar answered Oct 13 '22 00:10

Habib


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 */ }
like image 36
plinth Avatar answered Oct 13 '22 00:10

plinth