Export (0) Print
Expand All

Form Event Changes in Visual Basic .NET

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

In Visual Basic 6.0, the Initialize event was used to execute code before a form was loaded. In Visual Basic .NET, 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()
End Sub

' Visual Basic .NET 
Public Sub New()
    ' This call is required by the Windows Forms Designer.
    ' Add your initialization code here.
End Sub

In Visual Basic 6.0, the Terminate event was used to execute code after a form was unloaded. In Visual Basic .NET, 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. Note that a static variable is used to prevent the termination code from executing twice — this is necessary because the Dispose method is public and might be called outside of the form's code.

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

' Visual Basic .NET 
Public Overrides Sub Dispose(ByVal Disposing As Boolean)

    Static fTerminated As Boolean
    If Not fTerminated Then
        fTerminated = True
    End If
    ' The following code was automatically added by the Dispose method.
    If Disposing Then
        If Not components Is Nothing Then
        End If
    End If
End Sub

In Visual Basic 6.0, the Unload event had a Cancel argument; in Visual Basic .NET, 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.

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

See Also

Form Object Changes in Visual Basic .NET | Form Constructor | Form.Closing Event

© 2014 Microsoft