Search Tools Links Login

Secret of QueryUnload event


Visual Basic 6, or VB Classic

Determine when your app is being closed by Windows OS, or by Task Manager or by user.
This can be useful when you have to do cleaning up before your app exits.

Original Author: Chinese Guy

Code

'///////////////////////////////////////


'Excerpt from MSDN documentation.


'///////////////////////////////////////


'vbFormControlMenu 0 : The user chose the Close command from the Control menu on the form.


'vbFormCode 1 : The Unload statement is invoked from code.


'vbAppWindows 2 : The current Microsoft Windows operating environment session is ending.


'vbAppTaskManager 3 : The Microsoft Windows Task Manager is closing the application.


'Remarks


'This event is typically used to make sure there are no unfinished tasks in the forms
'included in an application before that application closes.
'For example, if a user has not yet saved some new data in any form,
'your application can prompt the user to save the data.


'When an application closes, you can use either the QueryUnload or Unload
'event procedure to set the Cancel property to True, stopping the closing process.
'However, the QueryUnload event occurs in all forms before any are unloaded,
'and the Unload event occurs as each form is unloaded.



Private Sub Command1_Click()


   Unload Me


End Sub


Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)


   Select Case UnloadMode


   Case vbFormControlMenu


      'The user clicked the Close button on the upper-right of form


      If MsgBox("UnloadMode : Close button. Wanna exit?", vbYesNo) = vbNo Then
         Cancel = True


      End If


   Case vbFormCode


      'There's Unload statement in the code.


      If MsgBox("UnloadMode : Unload statement. Wanna exit?", vbYesNo) = vbNo Then
         Cancel = True


      End If


   Case vbAppWindows


      'Windows OS session is ending.


      If MsgBox("UnloadMode : Windows OS. Wanna exit?", vbYesNo) = vbNo Then
         Cancel = True


      End If


   Case vbAppTaskManager


      'Windows Task Manager is closing this app.


      If MsgBox("UnloadMode : Task Manager. Wanna exit?", vbYesNo) = vbNo Then
         Cancel = True


      End If


   End Select


End Sub


About this post

Posted: 2003-06-01
By: ArchiveBot
Viewed: 105 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.