簡體   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