This documentation is archived and is not being maintained.

CancelEventArgs Class

Provides data for a cancelable event.

For a list of all members of this type, see CancelEventArgs Members.


[Visual Basic]
Public Class CancelEventArgs
   Inherits EventArgs
public class CancelEventArgs : EventArgs
public __gc class CancelEventArgs : public EventArgs
public class CancelEventArgs extends EventArgs

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.


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.

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


[Visual Basic, C#, C++] The following example uses a 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 myDataIsSaved.

[Visual Basic] 
' Calls this method from the InitializeComponent() method of your form.
Private Sub OtherInitialize()
    AddHandler Me.Closing, AddressOf Me.Form1_Cancel
    Me.myDataIsSaved = New Boolean()
    Me.myDataIsSaved = True
End Sub 'OtherInitialize

Protected Sub Form1_Cancel(sender As Object, e As CancelEventArgs)
    If Not myDataIsSaved Then
        e.Cancel = True
        MessageBox.Show("You must save first.")
        e.Cancel = False
    End If
End Sub 'Form1_Cancel

// Calls this method from the InitializeComponent() method of your form
    private void OtherInitialize() {
       this.Closing += new CancelEventHandler(this.Form1_Cancel);
       this.myDataIsSaved = new Boolean();
       this.myDataIsSaved = true;
    protected void Form1_Cancel (Object sender, CancelEventArgs e) {
       if (!myDataIsSaved) {
          e.Cancel = true;
          MessageBox.Show("You must save first.");
       else {
          e.Cancel = false;

// Calls this method from the InitializeComponent() method of your form

void OtherInitialize() {
    this->Closing += new CancelEventHandler(this, &Form1::Form1_Cancel);
    this->myDataIsSaved = true;


void Form1_Cancel (Object* /*sender*/, CancelEventArgs* e) {
    if (!myDataIsSaved) {
        e->Cancel = true;
        MessageBox::Show(S"You must save first.");
    else {
        e->Cancel = false;

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Namespace: System.ComponentModel

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System (in System.dll)

See Also

CancelEventArgs Members | System.ComponentModel Namespace | CancelEventHandler