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

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

Andere Tipps

Event with FormClosingEventArgs is only suitable for Form.FormClosing event. Change your code to this:

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
    }
}

You are trying to set an event handler for a FormClosing event to the click event of a label.
The two events have different signatures and thus are incompatible.

label_click(object sender, EventArgs e)

MyForm_FormClosing(object sender, FormClosingEventArgs e)

If your intention is to close the application when the user clicks on the label then you need to change your event handler to

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();
    }
}

if your intention is to ask for a confirmation whenever the user tries to exit the application then you need to add a FormClosing event handler to your form and then write

 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;
         }

     }
 }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top