This documentation is archived and is not being maintained.

CancelEventArgs.Cancel Property

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

[Visual Basic]
Public Property Cancel As Boolean
public bool Cancel {get; set;}
public: __property bool get_Cancel();
public: __property void set_Cancel(bool);
public function get Cancel() : Boolean;
public function set Cancel(Boolean);

Property Value

true if the event should be canceled; otherwise, false.


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


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

See Also

CancelEventArgs Class | CancelEventArgs Members | System.ComponentModel Namespace