CancelEventArgs 類別

定義

提供可取消事件的資料。

public ref class CancelEventArgs : EventArgs
public class CancelEventArgs : EventArgs
type CancelEventArgs = class
    inherit EventArgs
Public Class CancelEventArgs
Inherits EventArgs
繼承
CancelEventArgs
衍生

範例

下列範例會使用 CancelEventArgsCancelEventHandler 來處理 ClosingForm 事件。 此程式碼假設您已使用名為 isDataSaved 的類別層級 Boolean 變數建立 Form 。 它也假設您已新增 語句,以 OtherInitialize 在呼叫) 之後,從表單的 Load 方法或建構函式 (叫 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

注意

事件 Closing 已被取代,且已由 FormClosing 取代。 此處僅提供作為範例,以說明 的使用方式 CancelEventArgs

CancelEventArgsCancel提供 屬性,指出是否應該取消事件。

建構函式

CancelEventArgs()

CancelEventArgs 屬性設定成 false 來初始化 Cancel 類別的新執行個體。

CancelEventArgs(Boolean)

CancelEventArgs 屬性設定成指定值來初始化 Cancel 類別的新執行個體。

屬性

Cancel

取得或設定值,這個值表示是否應該取消事件。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱