Solved

CancelEventArgs - how to use

Posted on 2007-03-30
2
359 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
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Receiving a string from a WebService Push 21 51
Advice in Xamarin 21 80
Store results in vb.net 3 23
No Data for DropDown List 2 26
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…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

831 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