我試圖創建一個event handler
,這樣當用戶選擇close
(x)按鈕時,它會提示用戶保存任何未保存的更改。這裏是我的C#
代碼:用於關閉Windows窗體應用程序的事件處理程序
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
。我想我不明白爲什麼這個功能不能輕易完成。
什麼是在這個最好的方法?
把一個破發點,以你的'MainFram_FormClosing'事件和檢查。 – Arshad
如果這很容易,它不會在堆棧上。當事件被觸發時,似乎持續執行CloseFileOperation。 – Jonathan