2016-03-03 65 views
1

我對C#編碼頗爲陌生,我試圖創建一個'取消'按鈕。我收到上面的錯誤消息。有什麼建議麼?提前致謝! 我的代碼:沒有爲'button3_click'匹配委託過載system.eventhandler

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Threading; 

namespace test 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
      this.FormClosing += new FormClosingEventHandler(button3_Click); 
     } 
     public void button3_Click(object sender, FormClosingEventArgs e) 
     { 
      e.Cancel = true; 
      this.Hide(); 
     } 
    } 
} 

回答

1

你的問題是有點混亂。其實你的代碼應該編譯得很好,因爲Form.FormClosing事件需要一個具有你的button3_Click具有的簽名的方法。

但是,這一切似乎並不是你實際上的目標。我想你想點擊處理程序添加到您的按鈕:

public Form1() 
{ 
    InitializeComponent(); 
    this.button3.Click += button3_Click; 
} 
private void button3_Click(object sender, EventArgs e) 
{ 
    this.DialogResult = DialogResult.Cancel; 
    this.Close(); 
} 

Click事件引發(顧名思義),當用戶點擊該按鈕。

FormClosingForm即將關閉時引發。您可以使用它(例如)要求用戶進行確認:

public Form1() 
{ 
    InitializeComponent(); 
    this.button3.Click += button3_Click; 
    this.FormClosing += Form1_FormClosing; 
} 
private void button3_Click(object sender, EventArgs e) 
{ 
    this.DialogResult = DialogResult.Cancel; 
    this.Close(); 
} 
private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
{ 
    e.Cancel = MessageBox.Show(this, "Do you really want to quit?", 
      "Quit?", MessageBoxButtons.YesNo) != DialogResult.Yes; 
} 

通過使用該FormClosingEventArgs.Cancel屬性你可以告訴Form關閉。

+0

非常感謝它現在使用你的提示工作正常。這個函數不會取消循環,對嗎?在那種情況下我必須使用中斷嗎?你有什麼建議? – Mocke

0

這是你所需要的

public Form1() 
    { 
     InitializeComponent(); 
    } 

    public void button3_Click(object sender, EventArgs e) 
    { 
     this.Hide(); 
    } 
+0

感謝這也可以,但它只隱藏窗體。它並沒有真正退出運行。 – Mocke

相關問題