CancelEventArgs.Cancel Property
.NET Framework 2.0
Gets or sets a value indicating whether the event should be canceled.
Namespace: System.ComponentModel
Assembly: System (in system.dll)
Assembly: System (in system.dll)
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 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." ); } }
// Calls this method from the InitializeComponent() method of your form
private void OtherInitialize()
{
this.add_Closing(new CancelEventHandler(this.Form1_Cancel));
this.myDataIsSaved = (boolean)new System.Boolean();
this.myDataIsSaved = true;
} //OtherInitialize
protected void Form1_Cancel(Object sender, CancelEventArgs e)
{
if (!(myDataIsSaved)) {
e.set_Cancel(true);
MessageBox.Show("You must save first.");
}
else {
e.set_Cancel(false);
MessageBox.Show("Goodbye.");
}
} //Form1_Cancel
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.Community Additions
ADD
Show: