次の方法で共有


CancelEventArgs クラス

定義

キャンセル可能なイベントのデータを提供します。

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

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

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
void OtherInitialize()
{
    Closing += Form1_Closing;
    // Exchange commented line and note the difference.
    isDataSaved = true;
    //this.isDataSaved = false;
}

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

注釈

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

Closing イベントは非推奨となり、FormClosingに置き換えられました。 ここでは、 CancelEventArgsの使用方法を示すだけの例として提供されています。

CancelEventArgs は、イベントを取り消す必要があるかどうかを示す Cancel プロパティを提供します。

コンストラクター

名前 説明
CancelEventArgs()

Cancel プロパティを false に設定して、CancelEventArgs クラスの新しいインスタンスを初期化します。

CancelEventArgs(Boolean)

Cancel プロパティを指定した値に設定して、CancelEventArgs クラスの新しいインスタンスを初期化します。

プロパティ

名前 説明
Cancel

イベントを取り消す必要があるかどうかを示す値を取得または設定します。

メソッド

名前 説明
Equals(Object)

指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Objectの簡易コピーを作成します。

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

適用対象

こちらもご覧ください