简体   繁体   English

具有FormClosingEventArgs的EventHandler

[英]EventHandler with FormClosingEventArgs

I have this closing form code in my Form.cs 我的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(); 
        }
    }

and this code in my Form.designer.cs 和这段代码在我的Form.designer.cs中

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

However it keeps showing error "No overload for 'label7_Click' matches delegate 'System.EventHandler'" 但是,它始终显示错误“'label7_Click'没有重载匹配委托'System.EventHandler'”

What should I do? 我该怎么办?

It seems label7_Click method dose not exist 似乎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(); 
    }
    }

No overload for 'label7_Click' matches delegate

public void label7_Click(object sender, FormClosingEventArgs e)//this method de is incorrect

Your code is a bit confusing. 您的代码有点混乱。 Users click on label7 when they want to exit the application? 用户要退出应用程序时单击label7吗? The Click event that you are subscribing to does not provide FormClosingEventArgs when it is raised. 您订阅的Click事件在引发时不会提供FormClosingEventArgs。 The Click is an EventHandler event, which means it provides an EventArgs object when it's raised. Click是一个EventHandler事件,这意味着它在引发时会提供一个EventArgs对象。 There is no Cancel property in the EventArgs class. EventArgs类中没有Cancel属性。

It looks like you want to show a MessageBox when the user clicks on label7. 看起来您想在用户单击label7时显示一个MessageBox。 The MessageBox will ask the user, "Are you sure you want to exit?", and if the user clicks "yes" then the application will close. MessageBox将询问用户“您确定要退出吗?”,如果用户单击“是”,则应用程序将关闭。 If so, try: 如果是这样,请尝试:

private void label7_Click(object sender, EventArgs e)
{
    var result = MessageBox.Show("Are you sure you want to exit?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
    if (result == DialogResult.Yes)
    {
       Application.Exit();
    }
}

label7.Click += label7_Click;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM