CancelEventArgs - how to use
Posted on 2007-03-30
Can you explain in detail what is happening in this example from VB2003 help and what else I need to know about CancelEventArgs?
[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.
' 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 Sub 'Form1_Cancel