CancelEventArgs クラス

定義

キャンセルできるイベントのデータを提供します。

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

次の例では、 と をCancelEventHandler使用CancelEventArgsして のイベントをFormClosing処理します。 このコードでは、 という名前isDataSavedのクラス レベルBooleanの変数を使用して をForm作成していることを前提としています。 また、(の呼び出しの後に) フォームの Load メソッドまたはコンストラクターから メソッドを呼び出InitializeComponentOtherInitialize ステートメントを追加したことを前提としています。

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

注釈

キャンセル可能なイベントは、 のイベントなどClosingForm、取り消すことができるアクションを実行しようとしたときにコンポーネントによって発生します。

Note

イベントは 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)

適用対象

こちらもご覧ください