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. 此處提供的範例只是為了說明的使用CancelEventArgs方式。It 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 屬性設定成 false 來初始化 Cancel 類別的新執行個體。Initializes 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 的淺層複本 (Shallow Copy)。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)

適用於

另請參閱