How to handle close window event?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abhiab54
    New Member
    • Jan 2007
    • 10

    How to handle close window event?

    Hi all,
    Can anyone tell me which event get generated when I close window
    (e.g. of Form1) by cliking on its close button 'X' on rt handside on top of window.
    I need to execute perticular code when I close the window.
  • iburyak
    Recognized Expert Top Contributor
    • Nov 2006
    • 1016

    #2
    Put your code in Form_QueryUnloa d event.

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Originally posted by iburyak
      Put your code in Form_QueryUnloa d event.
      Yes, use the QueryUnload event if you want to have the option of cancelling (stopping) the unload. You can also place code in the Unload event (which fires after QueryUnload), but at that point you don't have the option to cancel the close.

      One nice thing about QueryUnload is that you receive a parameter telling you how/why the close was requested. So you can react differently depending on whether the user closed the form, or your code did it, or Windows is shutting down.

      Comment

      • hariharanmca
        Top Contributor
        • Dec 2006
        • 1977

        #4
        Originally posted by Killer42
        Yes, use the QueryUnload event if you want to have the option of cancelling (stopping) the unload. You can also place code in the Unload event (which fires after QueryUnload), but at that point you don't have the option to cancel the close.

        One nice thing about QueryUnload is that you receive a parameter telling you how/why the close was requested. So you can react differently depending on whether the user closed the form, or your code did it, or Windows is shutting down.

        We can use Form_Unload to cancel colseing form

        Code:
        dim boolClose  as Boolean
        
        Private Sub Form_Unload(Cancel As Integer)
            If boolClose = True Then
                Cancel = 0
                cmdClose_Click
            Else
                Cancel = 1
            End If
        End Sub

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Originally posted by hariharanmca
          We can use Form_Unload to cancel closing form...
          Sorry, my goof. Both events do have the Cancel option. I must have been getting confused with UnloadMode, which is only available in QueryUnload.

          Comment

          Working...