CancelEventArgs Class (System.ComponentModel) (original) (raw)
- Reference
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Provides data for a cancelable event.
public ref class CancelEventArgs : EventArgs
public class CancelEventArgs : EventArgs
type CancelEventArgs = class
inherit EventArgs
Public Class CancelEventArgs
Inherits EventArgs
Inheritance
Derived
Examples
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
).
private:
// Call this method from the InitializeComponent() method of your form
void OtherInitialize()
{
this->Closing += gcnew CancelEventHandler( this, &Form1::Form1_Cancel );
this->myDataIsSaved = true;
}
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." );
}
}
// Call this method from the constructor of your form
void OtherInitialize()
{
Closing += Form1_Closing;
// Exchange commented line and note the difference.
isDataSaved = true;
//this.isDataSaved = false;
}
void Form1_Closing(object sender, CancelEventArgs e)
{
if (!isDataSaved)
{
e.Cancel = true;
_ = MessageBox.Show("You must save first.");
}
else
{
e.Cancel = false;
_ = MessageBox.Show("Goodbye.");
}
}
' 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
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
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.
CancelEventArgs provides the Cancel property to indicate whether the event should be canceled.
Constructors
Properties
Cancel | Gets or sets a value indicating whether the event should be canceled. |
---|