PDA

Click to See Complete Forum and Search --> : ASK to cancel when form X button clicked


Magik
Jan 10th, 2000, 02:55 AM
How can I cause VB to ask if you really want to close a form when someone clicks the X in the upper right hand corner.

QWERTY
Jan 10th, 2000, 03:02 AM
This should do the job:

Private Sub Form_Unload(Cancel As Integer)
Dim retVal As Integer

retVal = MsgBox("Are you sure you want to exit?", vbYesNo, "Exit")
If retVal = vbYes Then
Cancel = 0
Else
Cancel = 1
End If
End Sub


------------------
Visual Basic Programmer
------------------
PolComSoft
You will hear a lot about it.

WadeD
Jan 10th, 2000, 03:02 AM
In the form_unload event, type:
Dim Response as Byte

Response = MsgBox("Are you sure you want to close?", vbYesNo + vbInformation)
If Response = vbNo Then
Cancel = True
End If

Magik
Jan 10th, 2000, 03:23 AM
Thank you! Both worked great...

MartinLiss
Jan 10th, 2000, 03:41 AM
You'd be better off if you did the following in your form's QueryUnload event. You may not need to check for all the conditions, but you probably should at least consider if you still want to ask the user if he/she wants to exit if it's Windows or your code that is closing the form.
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

Dim retVal As Integer

Select Case UnloadMode
Case vbFormControlMenu
' The user chose the Close command from the Control menu on the form
retVal = MsgBox("Are you sure you want to exit?", vbYesNo, "Exit")
If retVal = vbYes Then
Cancel = True
End If
Case vbFormCode
' The Unload statement is invoked from code.
Case vbAppWindows
' The current Microsoft Windows operating environment session
' is ending.
Case vbAppTaskManager
' The Microsoft Windows Task Manager is closing the application.
Case vbFormMDIForm
' An MDI child form is closing because the MDI form is closing.
vbFormOwner
' A form is closing because its owner is closing.
End Select

End Sub


------------------
Marty

Magik
Jan 10th, 2000, 04:09 AM
Marty,

Thank you! That is what I will use as it would make sense to make sure you know what is telling the form to close.