Solved

CancelEventArgs - how to use

Posted on 2007-03-30
2
363 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 18825649
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
ID: 18825805
Much clearer than the help example. Thanks!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 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