A Visual Basic form normally passes through four states in its lifetime:
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.
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.
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:
Form1.Show
.*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
.
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:
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.
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.
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.
This approach uses a helper class to expose the load and unload functionality
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
This mechanism works better when the original VB6 application has some situations where Form_Load is invoked before the form is shown.
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.
8834 N Capital of Texas Hwy, Ste 302
Austin, TX 78759
Call us: +1 (425) 609-8458
info@wearegap.com