【发布时间】:2013-11-01 03:52:25
【问题描述】:
我正在尝试创建一个event handler,这样当用户选择close (x) 按钮时,它会提示用户保存任何未保存的更改。这是我的C# 代码:
private void CloseFileOperation()
{
// If the Spreadsheet has been changed since the user opened it and
// the user has requested to Close the window, then prompt him to Save
// the unsaved changes.
if (SpreadSheet.Changed)
{
DialogResult UserChoice = MessageBox.Show("Would you like to save your changes?", "Spreadsheet Utility",
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
switch (UserChoice)
{
case DialogResult.Yes:
SaveFileOperation();
this.Close();
break;
case DialogResult.No:
this.Close();
break;
case DialogResult.Cancel:
return;
}
}
// If the Spreadsheet hasn't been changed since the user opened it, then
// simply Close the window.
else
this.Close();
}
我创建了当用户选择关闭 (x) 按钮时触发的事件处理程序 MainFram_FormClosing。
private void MainFrame_FormClosing(object sender, FormClosingEventArgs e)
{
// Close the Spreadsheet.
CloseFileOperation();
}
每当我选择关闭按钮时,应用程序就会崩溃。我已经阅读了来自this 帖子的回复。我想我违反了Windows 7 Program Requirements。我想我不明白为什么这个功能不能这么容易地完成。
这方面最好的方法是什么?
【问题讨论】:
-
在
MainFram_FormClosing事件中设置一个断点并检查。 -
如果这么简单,它就不会出现在 Stack 上。事件触发的时候好像一直在执行CloseFileOperation。
标签: c# winforms forms validation savechanges