CancelEventArgs CancelEventArgs CancelEventArgs CancelEventArgs Class

定义

为可取消的事件提供数据。Provides data for a cancelable event.

public ref class CancelEventArgs : EventArgs
public class CancelEventArgs : EventArgs
type CancelEventArgs = class
    inherit EventArgs
Public Class CancelEventArgs
Inherits EventArgs
继承
CancelEventArgsCancelEventArgsCancelEventArgsCancelEventArgs
派生

示例

下面的示例使用CancelEventArgs CancelEventHandlerClosingForm处理的事件。The following example uses CancelEventArgs and a CancelEventHandler to handle the Closing event of a Form. 此代码假定你已使用名为Form isDataSaved的类级Boolean变量创建了。This code assumes that you have created a Form with a class-level Boolean variable named isDataSaved. 它还假定您已添加了一个语句, 用于从OtherInitialize窗体的Load方法或构造函数InitializeComponent(在调用之后) 调用方法。It also assumes that you have added a statement to invoke the OtherInitialize method from the form's Load method or the constructor (after the call to InitializeComponent).

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." );
      }
   }
// 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.");
       }
    }
 
' 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

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

注解

当某个组件要执行可取消的操作 (如Closing的事件Form) 时, 该组件会引发该事件。A cancelable event is raised by a component when it is about to perform an action that can be canceled, such as the Closing event of a Form.

备注

Closing事件已弃用, 并且已被FormClosing替换。The Closing event is deprecated and has been replaced by FormClosing. 此处提供的示例仅用于说明的用法CancelEventArgsIt is offered as an example here only to illustrate the usage of CancelEventArgs.

CancelEventArgsCancel提供属性以指示是否应取消事件。CancelEventArgs provides the Cancel property to indicate whether the event should be canceled.

构造函数

CancelEventArgs() CancelEventArgs() CancelEventArgs() CancelEventArgs()

初始化 CancelEventArgs 类的新实例,其 Cancel 属性设置为 falseInitializes a new instance of the CancelEventArgs class with the Cancel property set to false.

CancelEventArgs(Boolean) CancelEventArgs(Boolean) CancelEventArgs(Boolean) CancelEventArgs(Boolean)

初始化 CancelEventArgs 类的新实例,其 Cancel 属性设置为给定值。Initializes a new instance of the CancelEventArgs class with the Cancel property set to the given value.

属性

Cancel Cancel Cancel Cancel

获取或设置指示是否应取消事件的值。Gets or sets a value indicating whether the event should be canceled.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

适用于

另请参阅