Form Events for Visual Basic 6.0 Users

There are some differences in form-event behavior between Visual Basic 6.0 and Visual Basic 2008.

Conceptual Differences

Initialize Event

In Visual Basic 6.0, the Initialize event is used to execute code before a form is loaded.

In Visual Basic 2008, initialization code must be added to the form constructor (Sub New()) after the call to InitializeComponent(), as in the following example:

' Visual Basic 6.0 
Private Sub Form_Initialize()
    MsgBox("The form is loading")
End Sub

 

' Visual Basic .NET PublicSubNew()
    ' This call is required by the Windows Form Designer.
    InitializeComponent()
    ' Add any initialization after the InitializeComponent() call.
    MsgBox("The form is loading")
EndSub

Note

A common use of the Initialize event is to show a "splash" form to display information while the form is loading. Visual Basic 2008 applications have a SplashScreen property that can be used to automatically display a form during application startup. For more information, see How to: Specify a Splash Screen for an Application (Visual Basic).

Terminate Event

In Visual Basic 6.0, the Terminate event is used to execute code after a form is unloaded.

In Visual Basic 2008, the Terminate event is no longer supported. Termination code must be executed inside the Dispose method, before the call to MyBase.Dispose().

Note

The Dispose method is called automatically for the main form in an application; you need to call it explicitly for any other form.

The following example demonstrates the differences.

' Visual Basic 6.0 
Private Sub Form_Terminate()
    MsgBox "The form was terminated"
End Sub

 

' Visual Basic .NET ProtectedOverloadsOverridesSub Dispose(ByVal disposing AsBoolean)
    If disposing AndAlso components IsNotNothingThen
        MsgBox("The form was terminated")
        components.Dispose()
    EndIfMyBase.Dispose(disposing)
EndSub

Unload Event

In Visual Basic 6.0, the Unload event has a Cancel argument; in Visual Basic 2008, it is replaced by the Closed event, which does not have a Cancel argument. If you need to cancel during unloading, use the Closing event instead.

Mouse Events for MDI Forms

In Visual Basic 6.0, MDI Forms support mouse events. In Visual Basic 2008, because an MDI Form has no client area to receive mouse events, the Click, MouseDown, MouseMove, and MouseUp events are no longer supported for MDI Forms.

See Also

Concepts

Form Object for Visual Basic 6.0 Users

Reference

Form

Form..::.Closing