2016-07-29 42 views
0

我有一個保存按鈕,我的WinForm保存了表單信息,然後使用this.Close()語句關閉表單。處理表單的兩種不同方式關閉事件

然而,還有另一種關閉窗體的方法,那就是X按鈕。

我正在使用FormClosing事件在表單關閉之前提出問題。

private void EmailNewsletter_FormClosing(object sender, FormClosingEventArgs e) 
{ 
    DialogResult dr = MsgBox.Show("Are you sure you want to dimiss this newsletter?", "Dismiss Newsletter", MsgBox.Buttons.YesNo, MsgBox.Icon.Question); 
    if (dr == System.Windows.Forms.DialogResult.Yes) 
    { 
     this.Newsletter = null; 
    } 
    else 
    { 
     e.Cancel = true; 
    } 
} 

但是處理關閉窗體的方式取決於窗體如何關閉。如果用戶點擊保存按鈕,則表單應該沒有問題地關閉。只有當用戶點擊X按鈕時,應該詢問該問題。

如何讓我的表單知道關閉命令來自哪裏?

+0

使用變量 「isFromSaveButton」 .... – 2016-07-29 13:18:57

回答

1

一個簡單的布爾標誌應該做的伎倆:

private bool saveClicked = false; 
private void btnSave_click(object sender, EventArgs e) 
{ 
     saveClicked = true; 
} 
private void EmailNewsletter_FormClosing(object sender, FormClosingEventArgs e) 
{ 
    if(saveClicked) 
     return; 

    DialogResult dr = MsgBox.Show("Are you sure you want to dimiss this newsletter?", "Dismiss Newsletter", MsgBox.Buttons.YesNo, MsgBox.Icon.Question); 

    if (dr == System.Windows.Forms.DialogResult.Yes) 
    { 
     this.Newsletter = null; 
    } 
    else 
    { 
     e.Cancel = true; 
    } 
} 
+0

謝謝!將在5分鐘內被接受爲答案,因爲我提高了你的心! – Disasterkid

相關問題