Edit

Share via


CancelEventArgs Class

Definition

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
Inheritance
CancelEventArgs
Derived

Examples

The following example uses CancelEventArgs and a CancelEventHandler to handle the Closing event of a Form. This code assumes that you have created a Form with a class-level Boolean variable named isDataSaved. 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

Remarks

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.

Note

The Closing event is deprecated and has been replaced by FormClosing. It is offered as an example here only to illustrate the usage of CancelEventArgs.

CancelEventArgs provides the Cancel property to indicate whether the event should be canceled.

Constructors

CancelEventArgs()

Initializes a new instance of the CancelEventArgs class with the Cancel property set to false.

CancelEventArgs(Boolean)

Initializes a new instance of the CancelEventArgs class with the Cancel property set to the given value.

Properties

Cancel

Gets or sets a value indicating whether the event should be canceled.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

See also