繁体   English   中英

e。取消不起作用

[英]e.Cancel does not work

我有VS 2010,想通过Yes | No | Cancel对话框取消表单关闭事件,但是当我将e.Cancel放入对话框的事件处理程序中时,出现错误消息“ System.EventArgs”不包含“取消”的定义,找不到扩展方法“取消”接受类型为“ System.EventArgs”的第一个参数(您是否缺少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;

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;

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

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

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

private void displayMessageBox(object sender, FormClosingEventArgs e)

和:

e.Cancel = true;

或强制引用以该类型访问它:

((FormClosingEventArgs)e).Cancel = true;

这很容易>>

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