Solved

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

Posted on 2004-08-15
2
162 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 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

16 Experts available now in Live!

Get 1:1 Help Now