2010-06-01 148 views
0

如何在窗體關閉事件中調用button1_Click事件,因此我不必從button1_Click複製和粘貼代碼?窗體關閉幫助

 public void button1_Click(object sender, EventArgs e) 
    { 
     //Yes or no message box to exit the application 
     DialogResult Response; 
     Response = MessageBox.Show("Are you sure you want to Exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); 
     if (Response == DialogResult.Yes) 

      // Exits the application 
      Application.Exit(); 
    } 

    public void xGameThemeComboBox_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     string folder = Application.StartupPath; 
     string theme = (string)xGameThemeComboBox.Items[xGameThemeComboBox.SelectedIndex]; 
     string path = System.IO.Path.Combine(folder, theme + ".jpg"); 
     Image newImage = new Bitmap(path); 
     if (this.BackgroundImage != null) this.BackgroundImage.Dispose(); 
     { 
      this.BackgroundImage = newImage; 
     } 

    } 

    private void xGameForm_FormClosing(object sender, FormClosingEventArgs e) 
    { 
     // call button1_Click here 
    } 

回答

4

你真正需要做的:

 private bool ShowClose() 
    { 
     //Yes or no message box to exit the application 
     DialogResult Response; 
     Response = MessageBox.Show("Are you sure you want to Exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); 
     return Response == DialogResult.Yes; 
    } 

     public void button1_Click(object sender, EventArgs e) 
    { 
     Close(); 
    } 

private void xGameForm_FormClosing(object sender, FormClosingEventArgs e) 
    { 
     e.Cancel = !ShowClose(); 
    } 
+0

-1:將顯示對話框兩次(一次用於按鈕單擊,一次用於Application.Exit'提升FormClosing事件 – Powerlord 2010-06-01 15:05:50

+0

@R Bemrose:Application.Exit不會增加FormClosing至i請記住 – Andrey 2010-06-01 15:09:20

+0

是的,我甚至在自己的答案中引用了文檔,並鏈接到它。 – Powerlord 2010-06-01 15:35:54

0
button1_Click(null, null); 
+0

感謝,但你能還告訴我,爲什麼消息框讓我打兩次是現在關閉表單? – 2010-06-01 14:51:36

1

更好地提取在button1_Click事件中的代碼放到一個方法,然後調用來自兩個事件的方法。

1

將按鈕方法的內容提取到它自己的方法中,然後從兩個點調用方法。

+0

Doh Sam一秒鐘打敗了我。 – nportelli 2010-06-01 14:51:18

0

你不應該顯示在MessageBox在所有的按鈕。

您可以簡單地在按鈕中調用Application.Exit(),並且FormClosing事件可以顯示確認消息。

0

Application.Exit提出的所有打開的窗體FormClosing事件:

一個FormClosing事件引發了 由 OpenForms屬性表示每一種形式。通過將其FormClosingEventArgs 參數的取消 屬性設置爲true,可以取消該事件 。

如果其中一個或多個處理程序取消 該事件,則Exit將返回沒有 的進一步操作。否則,將爲每個 打開表單引發一個事件 FormClosed,然後關閉所有正在運行的消息 循環和表單。

我會改寫這樣的:

public void button1_Click(object sender, EventArgs e) 
{ 
    // Exits the application 
    Application.Exit(); 
} 

private void xGameForm_FormClosing(object sender, FormClosingEventArgs e) 
{ 
    //Yes or no message box to exit the application 
    DialogResult Response; 
    Response = MessageBox.Show("Are you sure you want to Exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); 
    if (Response == DialogResult.No) 
    { 
     // Cancel the close, prevents applications from exiting. 
     e.Cancel = true; 
    } else { 
     Application.Exit(); 
    } 

} 
+0

我做了重寫,但現在沒有任何反應,表單不會關閉 – 2010-06-01 15:23:11

+0

這是不尋常的。我測試了這個代碼,並且它在我的Visual Studio 2008 SP1中的示例.NET 2.0項目中工作正常。 – Powerlord 2010-06-01 15:34:28

+0

哦,我應該在我的最後一個評論中提出這個問題:您確實注意到「Response =='被翻轉了檢查'DialogResult.No'而不是'Yes'? – Powerlord 2010-06-01 15:38:24