Freetutes.com

VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

VB .NET - Learn Visual Basic .NET

Systems Analysis - System analysis and Design tutorial for Software Engineering


You are here: Visual Basic > Advanced VB6 tutorial > Chapter 4

Loading and Unloading Forms in Visual Basic 6 (VB6)

Visual Basic projects have a special object that can be automatically loaded when the program is run. This object is referred to as the Startup object. Figure 4.20 shows an example of a Startup object. The Startup object can now vary depending on the type of project you are creating. A form can be selected, the Sub Main procedure, or— new in VB5 and VB6—nothing. You would specify Nothing when the program does not require an interface. An example of this type of project might be an ActiveX component or an ActiveX control.

When a form is specified as the Startup object, that form automatically loads into memory when the application starts. No other form loads unless it is referenced in program code or is explicitly loaded into memory.

Use the Load statement to load a form into memory without making it visible yet. The Load statement will take only one argument: the name of the object to be loaded. Take a look at the following code:

Load Form1
Load frmTest

Selecting the project Startup object.
FIGURE 4.20 Selecting the project Startup object.

The Load statement in both cases accepts a valid object name. This causes the object to load into memory. Although the object loads into memory, this does not mean that the object will be visible to the user. This enables the programmer to load multiple forms that may be required and to prepare them with information before display.

Once loaded into memory, the form's controls and any program code can be used. When working with forms in VB, it is important to note that any reference to an object will cause that object to load. The Load statement does not have to be explicitly used before an object can be used. An example of this would be if a form's Caption property were set, as follows:

Form1.Caption = "My Notepad"

Notice that this is the only line of code that you need to be concerned with. There is no Load statement before the Caption property is set. This code directly sets the form's property. This single line of code automatically causes the Form1 object to be loaded into memory. This is often referred to as implicit loading. Because the object must be loaded to set the property, VB does exactly that.

Implied loading can often cause problems when working on a multiform project. The programmer does not notice that one form calls or sets information on another form. The form then automatically loads. Later when you attempt to unload by name all forms that you remember using, your project continues to run.

The End statement can be used to force the application to terminate, regardless of which forms were explicitly or implicitly loaded.

This is a fail-safe in case the program missed a reference or miscounted how many forms were loaded.

However, the End statement will have an undesirable effect because it will end the application so abruptly that the QueryUnload and Unload events of forms will not have a chance to run.

For a more acceptable method to unload all forms, see the section in this chapter "Using the Forms Collection to Unload All Forms."

When an individual form is no longer required, you can unload it from memory. This will release the graphic components from memory.

The following code unloads two forms:

Unload Form1
Unload frmTest

The Unload statement accepts a valid object name. This causes the design time graphic components of a form to be released. Although the form has been unloaded from memory, it is very important to note that any Public procedures or variables belonging to the form are still in memory. The graphical controls are no longer available, but any Public members of the form can still be called, using the syntax for calling any object variable's members, formname.procname.

Remember, a form's Public procedures and variables are considered to be members (i.e., methods and properties) of the instance of the form object. Of course what's just been said for Public variables also goes for a form's Custom Properties implemented with Property Get and Property Let/Set procedures.

Load and Unload are used to control the memory status of a form. These two statements always appear before the name of the object to be affected. They are often confused with the Show and Hide methods that take place after the object name. Show and Hide are used to control whether a form is visible to the user.

After you unload a form, the form's Public procedures and its Public variables will still be resident as mentioned above. To completely reset the contents of these elements, call the line

Set FormName = Nothing

in your code just after calling the Unload statement.

Creating Data Input Forms and Dialog Boxes topics

See Also


  

<< Previous | Contents | Next >>

Home | Link to Us | About Us | Privacy Policy | Contact Us

Copyright © Freetutes.com | All Rights Reserved