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

雨燕双飞 提交于 2020-01-03 08:53:12

问题


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?


回答1:


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 */ }



回答2:


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


来源:https://stackoverflow.com/questions/25693300/how-to-do-something-if-cancel-button-on-save-file-dialog-was-clicked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!