我正在尝试创建一个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();
}
我创建MainFram_FormClosing
了当用户选择关闭 (x) 按钮时触发的事件处理程序。
private void MainFrame_FormClosing(object sender, FormClosingEventArgs e)
{
// Close the Spreadsheet.
CloseFileOperation();
}
每当我选择关闭按钮时,应用程序就会崩溃。我已经阅读了帖子的回复this
。我想我违反了Windows 7 Program Requirements
. 我想我不明白为什么这个功能不能这么容易地完成。
这方面最好的方法是什么?