1

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?

nobody
  • 19,814
  • 17
  • 56
  • 77
noobprogrammer
  • 1,140
  • 7
  • 22
  • 35
  • @dlev If I remove the `new System.EventHandler()` stuff, when I click on the label, it shows nothing because the function not called. – noobprogrammer Apr 11 '13 at 15:12

2 Answers2

0

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
KF2
  • 9,887
  • 8
  • 44
  • 77
  • If I change `public void label7_Click(object sender, FormClosingEventArgs e)` to `public void label7_Click(object sender, EventArgs e)` it keeps showing error because there's no method `e.Cancel` in EventArgs I'm on my way making exit confirmation from a form... – noobprogrammer Apr 11 '13 at 15:27
  • FormClosingEventArgs is only applicable to the form's [FormClosing](http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing.aspx) event. – Powerlord Apr 11 '13 at 15:30
  • I've tried your code but when I click 'No' or 'Yes', the message box shows once again then I click one of them, finally the form finally closed (yes) or back to the form (no). So, I must click twice to confirm the closing form. – noobprogrammer Apr 11 '13 at 15:35
0

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;
Jordan Wilcken
  • 316
  • 2
  • 10