CancelEventArgs 类

为可取消的事件提供数据。

**命名空间:**System.ComponentModel
**程序集:**System(在 system.dll 中)

语法

声明
Public Class CancelEventArgs
    Inherits EventArgs
用法
Dim instance As CancelEventArgs
public class CancelEventArgs : EventArgs
public ref class CancelEventArgs : public EventArgs
public class CancelEventArgs extends EventArgs
public class CancelEventArgs extends EventArgs

备注

可取消的事件由某个组件在其即将执行某项可取消的操作时引发,如 FormClosing 事件。

CancelEventArgs 提供 Cancel 属性来指示事件是否应取消。

提示

应用于此类的 HostProtectionAttribute 属性 (Attribute) 具有以下 Resources 属性 (Property) 值:SharedStateHostProtectionAttribute 不影响桌面应用程序(桌面应用程序一般通过双击图标,键入命令或在浏览器中输入 URL 启动)。有关更多信息,请参见 HostProtectionAttribute 类或 SQL Server 编程和宿主保护属性

示例

下面的示例使用 CancelEventArgsCancelEventHandler 来处理 FormClosing 事件。此代码假定已经创建具有类级别 Boolean 变量(名为 isDataSaved)的 Form。还假定已添加了一个语句,以从窗体的 Load 方法或构造函数(需先调用 InitializeComponent)调用 OtherInitialize 方法。

' Call this method from the Load method of your form.
Private Sub OtherInitialize()
    ' Exchange commented line and note the difference.
    Me.isDataSaved = True
    'Me.isDataSaved = False
End Sub 'OtherInitialize

Private Sub Form1_Closing(sender As Object, e As _
   System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    If Not isDataSaved Then
        e.Cancel = True
        MessageBox.Show("You must save first.")
    Else
        e.Cancel = False
        MessageBox.Show("Goodbye.")
    End If
End Sub 'Form1_Closing
// Call this method from the constructor of your form
    private void OtherInitialize() {
       this.Closing += new CancelEventHandler(this.Form1_Closing);
       // Exchange commented line and note the difference.
       this.isDataSaved = true;
       //this.isDataSaved = false;
    }

    private void Form1_Closing(Object sender, CancelEventArgs e) {
       if (!isDataSaved) {
          e.Cancel = true;
          MessageBox.Show("You must save first.");
       }
       else {
          e.Cancel = false;
          MessageBox.Show("Goodbye.");
       }
    }
 
private:
   // Call this method from the InitializeComponent() method of your form
   void OtherInitialize()
   {
      this->Closing += gcnew CancelEventHandler( this, &Form1::Form1_Cancel );
      this->myDataIsSaved = true;
   }

   void Form1_Cancel( Object^ /*sender*/, CancelEventArgs^ e )
   {
      if ( !myDataIsSaved )
      {
         e->Cancel = true;
         MessageBox::Show( "You must save first." );
      }
      else
      {
         e->Cancel = false;
         MessageBox::Show( "Goodbye." );
      }
   }
// Calls this method from the InitializeComponent() method of your form
private void OtherInitialize()
{
    this.add_Closing(new CancelEventHandler(this.Form1_Cancel));
    this.myDataIsSaved = (boolean)new System.Boolean();
    this.myDataIsSaved = true;
} //OtherInitialize

protected void Form1_Cancel(Object sender, CancelEventArgs e)
{
    if (!(myDataIsSaved)) {
        e.set_Cancel(true);
        MessageBox.Show("You must save first.");
    }
    else {
        e.set_Cancel(false);
        MessageBox.Show("Goodbye.");
    }
} //Form1_Cancel

继承层次结构

System.Object
   System.EventArgs
    System.ComponentModel.CancelEventArgs
       派生类

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

CancelEventArgs 成员
System.ComponentModel 命名空间
CancelEventHandler