CancelEventArgs::Cancel Property
Gets or sets a value indicating whether the event should be canceled.
Assembly: System (in System.dll)
The following example uses CancelEventArgs and a CancelEventHandler to handle the FormClosing 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." ); } }
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.