CancelEventArgs 類別

定義

提供可取消事件的資料。

C#
public class CancelEventArgs : EventArgs
繼承
CancelEventArgs
衍生

範例

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

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

備註

當元件即將執行可取消的動作時,元件就會引發可取消的事件,例如 Closing 的事件 Form

備註

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

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

建構函式

CancelEventArgs()

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

CancelEventArgs(Boolean)

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

屬性

Cancel

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

方法

Equals(Object)

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

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

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

(繼承來源 Object)

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

另請參閱