Visual Basic Reference

Visual Studio 6.0

Unload Event

See Also    Example    Applies To

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.


Private Sub object_Unload(cancel As Integer)

The Unload event syntax has these parts:

Part Description
Object An object 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.


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 the MDI form for which the current form is a child form, or exiting the Microsoft Windows operating environment while the application is running.