【问题标题】:e.Cancel does not worke.取消不起作用
【发布时间】: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#


【解决方案1】:

FormClosing event 具有 its own EventArgs subclass,您应该将其作为事件处理程序的参数:

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

此外,e.Cancel 是一个属性,您将其称为方法。括号需要去掉。

【讨论】:

    【解决方案2】:

    Form.FormClosing 使用FormClosingEventArgs 而不仅仅是EventArgs

    你需要使用:

    private void displayMessageBox(object sender, FormClosingEventArgs e)
    

    如果您使用较旧的Form.Closing 事件,则将其定义为CancelEventHandler,它使用CancelEventArgs,而不是EventArgs

    private void displayMessageBox(object sender, CancelEventArgs e)
    

    使用其中任何一个,您都可以:

     e.Cancel = true;
    

    【讨论】:

    • 是的,这也可以。实际上它是一个FormClosingEventArgs 对象,但它继承了CancelEventArgs
    • 它在问题中说这是一个FormClosing 事件。
    【解决方案3】:

    在方法签名中使用FormClosingEventArgs 类型:

    private void displayMessageBox(object sender, FormClosingEventArgs e)
    

    和:

    e.Cancel = true;
    

    或者将引用转换为该类型来访问它:

    ((FormClosingEventArgs)e).Cancel = true;
    

    【讨论】:

      【解决方案4】:

      很简单>>

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-18
        • 1970-01-01
        • 1970-01-01
        • 2018-05-11
        • 1970-01-01
        • 2021-10-24
        相关资源
        最近更新 更多