Results 1 to 6 of 6

Thread: ASK to cancel when form X button clicked

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 1999
    Location
    Cary, NC, USA
    Posts
    17

    Post

    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.

  2. #2
    Fanatic Member
    Join Date
    Oct 1999
    Location
    MA, USA
    Posts
    523

    Post

    This should do the job:
    Code:
    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.


  3. #3
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363

    Post

    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


  4. #4

    Thread Starter
    Junior Member
    Join Date
    Aug 1999
    Location
    Cary, NC, USA
    Posts
    17

    Post

    Thank you! Both worked great...

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,175

    Post

    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.
    Code:
    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

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Aug 1999
    Location
    Cary, NC, USA
    Posts
    17

    Post

    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.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width