![](/img/trans.png)
[英]Stop System.Windows.SplashScreen with WinForms from closing the app
[英]Closing Windows in WinForms
我在C#
WinForm
上有兩個按鈕( File-Close和Red X )。 我希望它們的行為方式相同,所以我創建了一個輔助方法,在觸發兩個不同事件時會調用該方法。 到目前為止,這就是我所擁有的。
private void CloseFileOperation(object e)
{
// If the user selected the exit buttton from the main title bar,
// then handle the closing event properly.
if (e is FormClosingEventArgs)
{
FormClosingEventArgs FormCloser = (FormClosingEventArgs)e;
// If the Spreadsheet has been changed since the user opened it and
// the user has requested to Close the window, then prompt them 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();
FormCloser.Cancel = false;
break;
case DialogResult.No:
FormCloser.Cancel = false;
break;
case DialogResult.Cancel:
FormCloser.Cancel = true;
return;
}
}
// If the Spreadsheet hasn't been changed since the user opened it, then
// simply Close the window.
else
FormCloser.Cancel = false;
}
// Otherwise the user must have selected the "Close" option from the File menu.
// Handle the event in the following manner.
else
{
// 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和文件關閉”選項時,我會調用此函數。 請注意,當用戶選擇“文件關閉”時,適當的操作將調用Close()操作,該操作又將調用close事件。 結果,警告對話框“您想保存更改”顯示兩次。
有沒有一種簡單的方法可以類似於我目前的操作,從而消除兩次顯示的窗口?
1.如果要處理表單關閉事件(無論是從RED X按鈕還是通過File-> Close按鈕),請處理Form_Closing事件,以便它是所有關閉事件唯一調用的地方。
2.如果用戶說是->保存FileOperation並保持原樣,則關閉表單。
注意:無需使用-> this.Close()關閉表單
3.如果用戶說“否”->保持原樣,則關閉表單。
注意:無需使用-> this.Close()關閉表單
4.如果用戶說“取消”->“此處不應該關閉表單”,則將FormClosingEventArgs參數的“ e”屬性“取消”更改為true,這樣將取消關閉操作並且表單將保持打開狀態。
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// 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();
break;
case DialogResult.No:
break;
case DialogResult.Cancel:
e.Cancel = true;
break;
}
}
}
而不是執行this.Close()
File->Close
而是使用您用於Red X的事件處理程序。
例如->
public void FileCloseHandler() {
RedXHandler() // instead of this.Close()
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.