Frage

I have this closing form code in my 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

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

However it keeps showing error "No overload for 'label7_Click' matches delegate 'System.EventHandler'"

What should I do?

War es hilfreich?

Lösung

Your code is a bit confusing. Users click on label7 when they want to exit the application? The Click event that you are subscribing to does not provide FormClosingEventArgs when it is raised. The Click is an EventHandler event, which means it provides an EventArgs object when it's raised. There is no Cancel property in the EventArgs class.

It looks like you want to show a MessageBox when the user clicks on label7. The MessageBox will ask the user, "Are you sure you want to exit?", and if the user clicks "yes" then the application will close. 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;

Andere Tipps

It seems label7_Click method dose not exist

  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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top