Duration (Pacific Standard Time):
To (Pacific Standard Time):
Impact:
  • None
User Action:
  • None
.NET Framework Class Library

CancelEventArgs Class

Provides data for a cancelable event.

System.Object
  System.EventArgs
    System.ComponentModel.CancelEventArgs

Namespace:  System.ComponentModel
Assemblies:   System.ComponentModel (in System.ComponentModel.dll)
  System (in System.dll)
Public Class CancelEventArgs _
	Inherits EventArgs

The CancelEventArgs type exposes the following members.

  Name Description
Public method Supported by the XNA Framework Supported by Portable Class Library CancelEventArgs() Initializes a new instance of the CancelEventArgs class with the Cancel property set to false.
Public method Supported by the XNA Framework Supported by Portable Class Library CancelEventArgs(Boolean) Initializes a new instance of the CancelEventArgs class with the Cancel property set to the given value.
Top
  Name Description
Public property Supported by the XNA Framework Supported by Portable Class Library Cancel Gets or sets a value indicating whether the event should be canceled.
Top
  Name Description
Public method Supported by the XNA Framework Supported by Portable Class Library Equals(Object) Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected method Supported by the XNA Framework Supported by Portable Class Library Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method Supported by the XNA Framework Supported by Portable Class Library GetHashCode Serves as the default hash function. (Inherited from Object.)
Public method Supported by the XNA Framework Supported by Portable Class Library GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method Supported by the XNA Framework Supported by Portable Class Library MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method Supported by the XNA Framework Supported by Portable Class Library ToString Returns a string that represents the current object. (Inherited from Object.)
Top

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 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.

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).

' 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 'OtherInitialize

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 'Form1_Closing

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.