Solved

CancelEventArgs - how to use

Posted on 2007-03-30
2
352 Views
Last Modified: 2012-08-14
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.

[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
0
Comment
Question by:bobinorlando
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
Comment Utility
The example is quite silly...why add code in the IntializeComponent() method?

People normally just select the "Closing" event in the IDE and the handler is written for you.

Hopefully the code below illustrates the idea a bit better.

When the user clicks the close button, we check the myDataIsSaved flag to see if the data has been "saved" yet.

If not, we cancel the closing of the form by setting the Cancel parameter of the "e" argument to True.  This keeps the form open as if the "X" in the top right was never clicked.

To "save" the data, click the Button.  The myDataIsSaved flag will be set to True.

Now when the form is closed a "Goodbye" message will be saved and the form allowed to close as normal.

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private myDataIsSaved As Boolean = False

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MessageBox.Show("Data Saved")
        myDataIsSaved = True
    End Sub

    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If Not myDataIsSaved Then
            e.Cancel = True
            MessageBox.Show("You must save first.")
        Else
            ' e.Cancel = False <--- No need to do this...False is the default!
            MessageBox.Show("Goodbye.")
        End If
    End Sub

End Class
0
 
LVL 1

Author Comment

by:bobinorlando
Comment Utility
Much clearer than the help example. Thanks!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now