The Load and Unload statements
cause a form to load into memory or to unload
from memory respectively. Both statements take
the name of a form or a form variable as their
parameter. For example, the statement
Load frmMain
would cause frmMain to load into
memory, and the statement
Unload frmMain
would take it out of memory.
Although the Load statement brings
a form into memory, it doesn't make the
form visible. You must call the form's
Show method or set its Visible property to True
in order to make it visible to the user.
You might ask, since calling the
Show method or setting the Visible property
will load the form anyway, why bother ever using
the Load statement?
You might also ask, since calling
the Unload statement would make the form invisible
anyway, why bother to ever use the Hide method?
VB provides programmers with both Load and Unload statements and
Show and Hide methods because there are two different strategies for form management
in an application. Which strategy you choose depends on how your application needs
to balance speed of operation with efficient use of memory. The two strategies
are:
-
Fast. Load all the forms
you'll need in your application when
your application begins to run. While the
application is running, use only the Show
and Hide methods of forms. Use only the
Unload statement when your application is
ending.
-
Memory Efficient. Load
a form (either with the Show method or a
combination of the Load statement and Show
method) only when you need to use it. Immediately
unload a form as soon as you don't
need it in the application.
When you look at the Fast strategy,
it might appear that there would be a big delay
at the beginning of the program as your application
loaded all the forms it was going to use. While
this is objectively true, programmers usually
cover up for the fact by supplying a "splash"
screen to show the user flashy graphics as the
application loads the forms. This is such a
common technique in Windows programming that
users have come to accept and even expect a
delay of several seconds when a program begins
to run. Once the forms are loaded, there will
be no further delays as the application runs
(because no forms will need to be loaded).
In reality, you'll find
yourself using a combination of both the Fast
and Memory Efficient strategies. Not every VB
application, for example, will be able to load
and maintain all of its forms in memory throughout
the entire session of the application.
NOTE : Another Use of the Hide Method
Even in the Memory Efficient model described here, there is a good reason to use
Hide when the form needs to disappear instead of calling Unload immediately:
Consider the situation where you've loaded a form modally from elsewhere in your
code. When the user dismisses the form, you might want to check the state of some
of the form's controls by following these steps: From inside the modal form, you
should close it with the Hide method. In your calling code, check the information
you need from the form. Only then should you unload the form.