VB6 Form Load Different Behavior in VB.NET or C#

Form Load Event in VB6

A Visual Basic form normally passes through four states in its lifetime:

  1. Created, but not loaded.
  2. Loaded, but not shown.
  3. Shown.
  4. Memory and resources completely reclaimed.

There's a fifth state a form can get into under certain circumstances: Unloaded and unreferenced while a control is still referenced.

From MS Documentation

Most of the following content comes from the MS documentation, but it is copied here in case the page is no longer available.

Created, But Not Loaded

The beginning of this state is marked by the Initialize event. Code you place in the Form_Initialize event procedure is therefore the first code that gets executed when a form is created.

In this state, the form exists as an object, but it has no window. None of its controls exist yet. A form always passes through this state, although its stay there may be brief.

The following code creates an instance of Form1 without advancing the form to the loaded state:

Dim frm As Form1
Set frm = New Form1

In this state the only procedures you can execute without forcing the form to load are Sub, Function, and Property procedures you've added to the form's code window. 

For example, you might add the following method to Form1:

Public Sub ANewMethod()
Debug.Print "Executing ANewMethod"
End Sub

You could call this method using the variable frm (that is, frm.ANewMethod) without forcing the form on to the next state. 
You can execute as many custom properties and methods as you like without forcing the form to load. However, the moment you access one of the form's built-in properties, or any control on the form, the form enters the next state.

Loaded, But Not Shown

The event that marks the beginning of this state is the familiar Load event. Code you place in the Form_Load event procedure is executed as soon as the form enters the loaded state.

When the Form_Load event procedure begins, the controls on the form have all been created and loaded, and the form has a window — complete with window handle (hWnd) and device context (hDC) — although that window has not yet been shown.

Any form that becomes visible must first be loaded.

Many forms pass automatically from the Created, But Not Loaded state into the Loaded, but Not Shown state. 

A form will be loaded automatically if:

  • The form has been specified as the Startup Object, on the General tab of the Project Properties dialog box.
  • The Show method is the first property or method of the form to be invoked, as for example Form1.Show.
  • The first property or method of the form to be invoked is one of the form's built-in members, as for example the Move method.

*Note* This case includes any controls on the form, because each control defines a property of the form; that is, in order to access the Caption property of Command1, you must go through the form's Command1 property: Command1.Caption.

  • The Load statement is used to load the form, without first using New or As New to create the form, as described earlier.

Forms That Are Never Shown

In the first two cases listed above, the form will continue directly on to the visible state, as soon as Form_Load completes. In the last two cases, the form will remain loaded, but not shown.

It has long been common coding practice in Visual Basic to load a form but never show it. This might be done for several reasons:

  • To use the Timer control to generate timed events.
  • To use controls for their functionality, rather than their user interface — for example, for serial communications or access to the file system.
  • To execute DDE transactions.

Always Coming Home

Forms return from the visible state to the loaded state whenever they're hidden. Returning to the loaded state does not re-execute the Load event, however. Form_Load is executed only once in a form's life.

Shown

Once a form becomes visible, the user can interact with it. Thereafter, the form may be hidden and shown as many times as you like before finally being unloaded.

Form Load Event in .NET 

In .NET the Form Load is executed when the form is displayed for the first time. So it can have some slight difference with the Form_Load in VB6. If in VB6 for example you do something like Form1.Caption = "Hi" that will trigger the Form_Load event even if the form is not shown.

This change can affect your form logic because you might have some forms that enter the Loaded but not shown state in a different way than your .NET version.

How does the VBUC can handle Form_Load migration ?

Screenshot of conversion options for VBUC

FormLoad mechanism upgraded using a helper

This approach uses a helper class to expose the load and unload functionality

Screenshot of form load mechanism upgrade

In this approach a special helper is used that makes sure that form load is executed as well as the code for the visual controls. This helpers is good to make sure that all visual controls are loaded and provides more compatibility with VB6

FormLoad event upgraded as method invoked from form constructor

This mechanism works better when the original VB6 application has some situations where Form_Load is invoked before the form is shown.

FormLoad event upgraded to .NET native event

This approach maps the VB6 Form_Load to the Windows Form Load event. If your code does not have situation where your forms execute the Form_Load before they are shown, or you want more native code (and don't mind doing some code modifications after migration) this option will be better.

Talk To An Engineer