Export (0) Print
Expand All

CancelEventArgs.Cancel Property

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

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

Property Value

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

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

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

Show:
© 2015 Microsoft