简体   繁体   中英

e.Cancel does not work

I have VS 2010 and want to cancel the form closing event via a Yes|No|Cancel dialog, but when I put the e.Cancel in the event handler for the dialog box, I get an error that says "'System.EventArgs' does not contain a definition for 'Cancel' and no extension method 'Cancel' accepting a first argument of type 'System.EventArgs' could be found (are you missing a using directive or an assembly reference?)." Also the word "Cancel" has a red line under it. Everything I've read online says this is the only way to cancel a FormClosing event. I tested the code in VS2008 and it does the same thing.

The code for the event handler is below:

private void displayMessageBox(object sender, EventArgs e)
        {
        DialogResult result = MessageBox.Show("Do you want to save the changes to the document before closing it?", "MyNotepad",MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
        if (result == DialogResult.Yes)
        {
            saveToolStripMenuItem_Click(sender, e);
        }
        else if (result == DialogResult.No)
        {
            rtbMain.Clear();
            this.Text = "Untitled - MyNotepad"; 
        }
        else if (result == DialogResult.Cancel)
        {
            // Leave the window open.
            e.Cancel() = true;


        }

Here are the usings (in the event it makes a difference):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

Form.FormClosing uses a FormClosingEventArgs instead of just EventArgs .

You need to use:

private void displayMessageBox(object sender, FormClosingEventArgs e)

If you use the older Form.Closing event, instead, it is defined as a CancelEventHandler , which uses CancelEventArgs , not EventArgs .

private void displayMessageBox(object sender, CancelEventArgs e)

Using either of these, you can then do:

 e.Cancel = true;

A FormClosing event has its own EventArgs subclass , which you should be taking as a parameter to your event handler:

private void displayMessageBox(object sender, FormClosingEventArgs e)
{
    DialogResult result = MessageBox.Show("Do you want to save the changes to the document before closing it?", "MyNotepad", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
    if (result == DialogResult.Yes)
    {
        saveToolStripMenuItem_Click(sender, e);
    }
    else if (result == DialogResult.No)
    {
        rtbMain.Clear();
        this.Text = "Untitled - MyNotepad"; 
    }
    else if (result == DialogResult.Cancel)
    {
        // Leave the window open.
        e.Cancel = true;
    }
}

Further, e.Cancel is a property, and you're calling it as one would a method. The parentheses need to be removed.

Either use the FormClosingEventArgs type in the method signature:

private void displayMessageBox(object sender, FormClosingEventArgs e)

and:

e.Cancel = true;

Or cast the reference to access it as that type:

((FormClosingEventArgs)e).Cancel = true;

It is very easy >>

Use FormClosing Event:

private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
    displayMessageBox(this, e);
}

private void displayMessageBox(object sender, FormClosingEventArgs e)
{
    DialogResult result = MessageBox.Show("Do you want to save the changes to the document before closing it?",
                                            "MyNotepad", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
    if (result == DialogResult.Yes)
    {
         saveToolStripMenuItem_Click(sender, e);
    }
    else if (result == DialogResult.No)
    {
           rtbMain.Clear();
        this.Text = "Untitled - MyNotepad";
    }
    else if (result == DialogResult.Cancel)
    {
        // Leave the window open.
        e.Cancel = true;


    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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