Hi,
What does the Form_Terminate event do versus the Form_Unload event?
And when should you use one or the other?
Thanks,
Al.
Printable View
Hi,
What does the Form_Terminate event do versus the Form_Unload event?
And when should you use one or the other?
Thanks,
Al.
From MSDN:
UNLOAD
Occurs when a form is about to be removed from the screen. When that form is reloaded, the contents of all its controls are reinitialized. This event is triggered by a user closing the form using the Close command on the Control menu or an Unload statement.
Syntax
The Unload event syntax has these parts:Code:Private Sub object_Unload(cancel As Integer)
Part Description
Object Anobject expression that evaluates to an object in the Applies To list.
Cancel Integer that determines whether the form is removed from the screen. If cancel is 0, the form is removed. Setting cancel to any nonzero value prevents the form from being removed.
Remarks
Setting cancel to any nonzero value prevents the form from being removed, but doesn't stop other events, such as exiting from the Microsoft Windows operating environment. Use the QueryUnload event to stop exiting from Windows.
Use an Unload event procedure to verify that the form should be unloaded or to specify actions that you want to take place when the form is unloaded. You can also include any form-level validation code you may need for closing the form or saving the data in it to a file.
The QueryUnload event occurs before the Unload event. The Unload event occurs before the Terminate event.
The Unload event can be caused by using the Unload statement, or by the user choosing the Close command on a form's Control menu, exiting the application with the End Task button on the Windows Task List, closing theMDI form for which the current form is a child form, or exiting the Microsoft Windows operating environment while the application is running.
TERMINATE
Occurs when all references to an instance of a Form, MDIForm, User control, Property Page, Webclass, DHTML Page Designer, or class are removed from memory by setting all the variables that refer to the object to Nothing or when the last reference to the object falls out ofscope.
Syntax
The object placeholder represents anobject expression that evaluates to an object in the Applies To list.Code:Private Sub object_Terminate( )
Remarks
For all objects except classes, the Terminate event occurs after the Unload event.
The Terminate event isn't triggered if the instances of the form or class were removed from memory because the application terminated abnormally. For example, if your application invokes the End statement before removing all existing instances of the class or form from memory, the Terminate event isn't triggered for that class or form.
HTH
I still don't understand the difference.
My question asises because I'm playing around with the MSChart sample in VB6. It doesn't have a form_unload. Instead it has a File > Exit in the menu. This calls:
Does this perform the same function as Form_Unload?Code:Private Sub mnuExit_Click()
Unload Me
End Sub
There is also the following:
Couldn't all this be in the Form_Unload event?Code:Private Sub Form_Terminate()
Cleanup
End Sub
Public Sub Cleanup()
shtGas.Close
Set shtGas = Nothing
Set rngGas = Nothing
If ExcelWasNotRunning = True Then
appGas.Quit
End If
Set appGas = Nothing
End Sub
Thanks again,
Al.
Think of the form as consisting of 2 parts, the object part (the 'physical' form) and the code part (any sub/function you code in it).
UNLOAD unloads the the object part of the form from the memory. But the app still keep a reference to this form (in an intrinsic global variable with the same name as the form). In this state, the code part of the form is still in the memory.
TERMINATE 'destroys' the form completely. This happens when the reference to the form is removed (e.q. by Set Form1 = Nothing). After this also the code part of the form is removed from memory.
So the sequence is:
INITIALIZE - LOAD - UNLOAD - TERMINATE
And yes, i think your code can be put in the unload event.