【发布时间】:2012-07-30 19:51:19
【问题描述】:
我有 VS 2010 并想通过 Yes|No|Cancel 对话框取消表单关闭事件,但是当我将 e.Cancel 放入对话框的事件处理程序中时,我收到一条错误消息,提示“'System .EventArgs' 不包含'Cancel' 的定义,并且找不到接受'System.EventArgs' 类型的第一个参数的扩展方法'Cancel'(您是否缺少 using 指令或程序集引用?)。此外,“取消”一词下方有一条红线。我在网上阅读的所有内容都说这是取消 FormClosing 事件的唯一方法。我在 VS2008 中测试了代码,它做了同样的事情。
事件处理程序的代码如下:
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;
}
这里是用法(如果它有所作为):
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;
【问题讨论】:
-
Cancel不是一个方法调用它是一个属性。在此处阅读文档:msdn.microsoft.com/en-us/library/… -
如果你没听懂@asawyer 的意思,那就用不带括号的
e.Cancel = true;。 -
@EdS。如果他的代码编译了,他就不会在这里了。
-
我尝试了带和不带括号的 e.Cancel ,它抛出了同样的错误。
标签: c#