0

我的 Form.cs 中有这个结束表单代码

public void label7_Click(object sender, FormClosingEventArgs e)
    {
        MessageBox.Show("Are you sure you want to exit?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        if (MessageBox.Show("Are you sure you want to exit?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
        {
            e.Cancel = true;
        }
        else { 
            Application.Exit(); 
        }
    }

以及我的 Form.designer.cs 中的这段代码

this.label7.Click += new System.EventHandler(this.label7_Click);

但是它一直显示错误

“'label7_Click' 没有重载匹配委托 'System.EventHandler'”

我该怎么办?

4

3 回答 3

1

似乎 label7_Click 方法不存在

  this.label7.Click += new System.EventHandler(this.label7_Click);

    void label7_Click(object sender, EventArgs e)
    {

    if (MessageBox.Show("Are you sure you want to exit?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
    {
        //
    }
    else { 
        Application.Exit(); 
    }
    }

'label7_Click' 没有重载匹配委托

public void label7_Click(object sender, FormClosingEventArgs e)//this method de is incorrect
于 2013-05-17T12:41:07.187 回答
1

Event withFormClosingEventArgs仅适用于Form.FormClosingevent。将您的代码更改为:

public void label7_Click(object sender, EventArgs e)
{
    if (MessageBox.Show("Are you sure you want to exit?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
    {
        this.Close(); //this closes the form
    }
}
于 2013-05-17T12:41:30.420 回答
1

您正在尝试将 FormClosing 事件的事件处理程序设置为标签的单击事件。
这两个事件具有不同的签名,因此不兼容。

label_click(object sender, EventArgs e)

MyForm_FormClosing(object sender, FormClosingEventArgs e)

如果您打算在用户单击标签时关闭应用程序,那么您需要将事件处理程序更改为

label_click(object sender, EventArgs e)
{
    if (MessageBox.Show("Are you sure you want to exit?", "Confirmation", 
                      MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
    {
        this.Close();
    }
}

如果您的意图是在用户尝试退出应用程序时要求确认,那么您需要在表单中添加一个 FormClosing 事件处理程序,然后编写

 MyForm_FormClosing(object sender, FormClosingEventArgs e)
 {
     // NOTE, you don't want to abort closing when Windows shutdown, right?
     if(e.CloseReason == CloseReason.UserClosing)
     {
         if (MessageBox.Show("Are you sure you want to exit?", "Confirmation", 
                            MessageBoxButtons.YesNo, MessageBoxIcon.Question) == 
                            DialogResult.No)
         {
              e.Cancel = true;
         }

     }
 }
于 2013-05-17T12:41:30.517 回答