Export (0) Print
Expand All

CancelEventArgs Class

Provides data for a cancelable event.

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

System.Object
   System.EventArgs
      System.ComponentModel.CancelEventArgs
         System.Drawing.Printing.PrintEventArgs
         System.Windows.Forms.InputLanguageChangingEventArgs
         System.Windows.Forms.TreeViewCancelEventArgs

[Visual Basic]
Public Class CancelEventArgs
   Inherits EventArgs
[C#]
public class CancelEventArgs : EventArgs
[C++]
public __gc class CancelEventArgs : public EventArgs
[JScript]
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.

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.

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

Example

[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.")
    Else
        e.Cancel = False
        MessageBox.Show("Goodbye.")
    End If
End Sub 'Form1_Cancel

[C#] 
// 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;
          MessageBox.Show("Goodbye.");
       }
    }
 

[C++] 
// Calls this method from the InitializeComponent() method of your form
private:

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

protected:

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

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

Requirements

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

Show:
© 2014 Microsoft