Solved

How do I prevent a form from closing when the close button (X) is selected?

Posted on 2004-08-15
2
164 Views
Last Modified: 2010-04-23
Hi,
I want to be able to prevent an application from closing if the user did not save their data correctly. e.g. they missed completing a field properly.  I am using the  form_Closing sub but when I call exit sub the application still terminates.  How can I prevent this?

Private Sub myform_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If mbSaveNeeded Then
            If MsgBox("Save changes to current record?", MsgBoxStyle.Question.YesNo) = MsgBoxResult.Yes Then
                If Not DBSaveEntries() Then
                    Exit Sub THIS DOES NOT HELP BECAUSE THE FORM EXITS ANYWAYS
                End If
            End If
        End If
        mWB = Nothing
        End
    End Sub
0
Comment
Question by:fowlow
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 175 total points
ID: 11807242
To prevent closure of the form, set the Cancel property of the e variable passed into the Closing event to True like this:

    Private Sub myform_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If mbSaveNeeded Then
            If MsgBox("Save changes to current record?", MsgBoxStyle.Question.YesNo) = MsgBoxResult.Yes Then
                If Not DBSaveEntries() Then
                    e.Cancel = True ' <--------  This causes the Closing event to be cancelled                    
                End If
            End If
        End If
        mWB = Nothing
        End
    End Sub
0
 

Author Comment

by:fowlow
ID: 11809959
Exactly the answer I needed.
Thank-you very much.
Tim
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SLMGR Switches Are Not Working On KMS Host 3 92
How to use odbc in vb to connect to ms sql 14 52
VB.Net - TypeInitializer Error 25 26
vb.net class 3 12
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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