It might seem that using a current
form's Load event procedure to call another
form's methods or loading another form
implicitly or explicitly would be asking for
trouble.
However, it's perfectly
possible to do so, and in fact you can accomplish
some tasks in a simple and elegant fashion by
loading and activating a second form from a
first form's Load event procedure.
For instance, consider a form that is the main data input form
for an application. Perhaps this form needs a preliminary dialog box, such as
a login screen, just before it appears. One very simple way to achieve this effect
is by performing the following steps:
-
In the Load event of the main form, put
a modal call to the Show method of the login
dialog box form with a statement such as
Secondform.Show vbModal
-
Let the login dialog box expose a custom
flag property through a Public variable
or through Property procedures. This property
will be a Boolean value that indicates whether
or not the login has been successful.
-
Just before the login hides itself, it
sets the flag property appropriately.
-
The Load event procedure of the main form
has not finished running in all the time
that the login dialog box was running because
the login dialog box was running modally,
and therefore the main form's Load
event procedure was paused.
-
The code in the main form's Load
event that follows the modal call to the
login dialog box can check the dialog box
form's Boolean flag property to decide
what action to take. After retrieving this
information, the calling code in the main
form should manage memory efficiently by
setting the login dialog box form to Nothing.
-
If the main form decides not to unload
itself, then it will continue loading and
will become the application's active
form.
Perhaps the key point to bear
in mind is step 4 above: Actions you take to
manipulate other forms from within a given form's
Load event procedure have their effect regardless
of where you call them from. If you call the
secondary form modally (as described in step
1 above), then the current form_Load pauses
until the secondary form is dismissed.
Conversely, if you were to call
the Show method of a main form and you were
to call up a second form in a modeless state
from within the first form's Load event, using
code such as Secondform.Show vbModeless or simply
Secondform.Show
then the second form would load
implicitly (if it were not already loaded) and
would display.
However, the main form's Load event procedure would continue
to run (because the second form was modeless), and so the first form would eventually
load and display as well. The net result would be that you would end up with both
forms visible, and the first form would end up as the active form in the application.