[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

CancelEventArgs - how to use

Posted on 2007-03-30
2
Medium Priority
?
391 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 2000 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

650 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