简体   繁体   English

e。取消不起作用

[英]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?)." 我有VS 2010,想通过Yes | No | Cancel对话框取消表单关闭事件,但是当我将e.Cancel放入对话框的事件处理程序中时,出现错误消息“ System.EventArgs”不包含“取消”的定义,找不到扩展方法“取消”接受类型为“ System.EventArgs”的第一个参数(您是否缺少using指令或程序集引用?)。” 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. 我在网上阅读的所有内容均表明,这是取消FormClosing事件的唯一方法。 I tested the code in VS2008 and it does the same thing. 我在VS2008中测试了代码,它做了同样的事情。

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 . Form.FormClosing使用FormClosingEventArgs而不是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 . 如果使用较旧的Form.Closing事件,则将其定义为CancelEventHandler ,它使用CancelEventArgs而不是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: FormClosing事件具有自己的EventArgs子类 ,您应该将其作为事件处理程序的参数:

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. 此外, e.Cancel是一个属性,您将其称为方法。 The parentheses need to be removed. 括号需要删除。

Either use the FormClosingEventArgs type in the method signature: 在方法签名中使用FormClosingEventArgs类型:

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: 使用FormClosing事件:

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;


    }

}

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

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