CancelEventArgs 類別

定義

提供可取消事件的資料。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
繼承
CancelEventArgs
衍生

範例

下列範例會使用 CancelEventArgsCancelEventHandler 來處理的 Closing 事件 FormThe following example uses CancelEventArgs and a CancelEventHandler to handle the Closing event of a Form. 這段程式碼假設您已 Form 使用名為的類別層級變數來建立 Boolean isDataSavedThis code assumes that you have created a Form with a class-level Boolean variable named isDataSaved. 它也假設您已新增語句,以在 OtherInitialize 呼叫) 之後,從表單的 Load 方法或 (函式叫用方法 InitializeComponentIt 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 FormA 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事件已被取代,並已由取代 FormClosingThe Closing event is deprecated and has been replaced by FormClosing. 此處只提供範例說明的使用方式 CancelEventArgsIt is offered as an example here only to illustrate the usage of CancelEventArgs.

CancelEventArgs 提供 Cancel 屬性來指出是否應該取消事件。CancelEventArgs provides the Cancel property to indicate whether the event should be canceled.

建構函式

CancelEventArgs()

CancelEventArgs 屬性設定成 false 來初始化 Cancel 類別的新執行個體。Initializes a new instance of the CancelEventArgs class with the Cancel property set to false.

CancelEventArgs(Boolean)

CancelEventArgs 屬性設定成指定值來初始化 Cancel 類別的新執行個體。Initializes a new instance of the CancelEventArgs class with the Cancel property set to the given value.

屬性

Cancel

取得或設定值,這個值表示是否應該取消事件。Gets or sets a value indicating whether the event should be canceled.

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

適用於

另請參閱