The magazine of the Melbourne PC User Group

Seeing the Basics: Did you know?
Unloading Forms

Tony Stevenson

More interesting facts about VB

This month's column continues with a look at some of the different and interesting aspects of the Visual Basic language, all presented together under the general title of "Did You Know?"

Unloading forms when a program ends

Each form contained within a VB application requires its own chunk of memory. As your applications increase in complexity, and most likely require more forms, there's an increased likelihood that, when the application is terminated, some forms will not be unloaded. Forms left open like this consume memory that could be put to better use.

Instead of laboriously unloading each form by name, and increasing the risk of missing one, a preferred approach is to use the Forms collection object.

The Forms collection object is a collection automatically maintained by VB itself which contains information about each loaded form in an application.

To see how this object works, add four forms to a new, standard project. Then use VB's "Form Layout Window" to overlap the forms so that all four are visible when the application runs. The "Form Layout Window" is accessed by clicking the "View" menu option on the main VB design menu, followed by the "Form Layout Window" option. You can then use the mouse to click each of the forms in the Layout Window to move them to the desired positions.

Next, add two command buttons to the first form, and name them cmdShow and cmdEnd, with the respective captions of "Show" and "End". Make sure to arrange the forms using the "Form Layout Window" so you can see these two command buttons when the program runs.

The next step is to add the following code to the project:

Private Sub cmdShow_Click ()
Form2.Show
Form3.Show
Form4.Show
End Sub

Private Sub cmdEnd_Click ()
Dim i as Integer
For i = Forms.Count - 1 to 0_ Step -1
MsgBox "About to unload " &_ Forms(i).Caption
Unload Forms(i)
Next i
End Sub


Now run the application, and click the "Show" button to display forms 2, 3, and 4.

Click the "End" button, and the messages "About to unload Form4", "About to unload Form3", "About to unload Form2", and "About to unload Form1" should be displayed in sequence.

Run the application again, but this time just click the "End" button, and observe what happens.

There are some interesting points to notice about this code, and the best way to see what's happening is to set a breakpoint. A breakpoint is exactly what its name suggests--when VB gets to the statement which has been set as a breakpoint, it stops executing, and awaits further instruction from you as to what to do next.

To set a breakpoint on the "For i = Forms.Count - 1 to 0 Step -1" statement, open the VB code window to display the cmdEnd_Click () subroutine (double-click the "End" button when in design mode).

Now position the mouse cursor on the "For i = Forms.Count - 1 to 0 Step -1" statement (that is, click anywhere on that line), then click "Debug" on the main VB menu, followed by the "Toggle Breakpoint" option. The "For" statement should now be highlighted in a different colour.

Before you execute the program, make sure the "Debug" toolbar is displayed. Do this by clicking the "View" menu option on the main VB menu, followed by the "Toolbars" option. Make sure the "Debug" option is activated, that is, it should have a tick mark against it. If it doesn't, just click it with the mouse. The "Debug" toolbar will then be displayed.

Run the program, click the "Show" button, and then click the "End" button. The program will now stop executing at the "For" statement. Using the mouse, highlight the expression "Forms.Count" in the "For" statement, and then click the "Blue spectacles" icon on the "Debug" toolbar. The window which subsequently opens is the "Quick Watch" dialog, and it contains the value of "Forms.Count". At this stage of execution of the program, it will contain the value 4. This is what we would expect as there are four forms which have been loaded--the first form when the program started, and another 3 when the "Show" button was clicked.

Click the "Cancel" button to make the "Quick Watch" dialog disappear off your desktop.

To start the program running again, click the left most icon on the "Debug" toolbar which is the "Continue" command. Alternatively, to step through the program one line at a time, find the "Step Into" icon on the "Debug" toolbar and click it. The easiest way of locating the "Step Into" icon is to position your mouse cursor over each of the icons on the "Debug" toolbar and wait momentarily until the little yellow tooltip automatically pops up showing a short description of the icon. Click the "Step Into" icon now to advance the program one statement.

Note: to remove the breakpoint which has been set in the program, click "Debug" on the main VB menu, and click the option "Clear All Breakpoints".

So after the "For" statement is executed for the first time, the value contained in the variable "i" is 3. However, the value in the expression "Forms(i).Caption" is "Form4". The explanation for this is that the Forms collection object starts at 0 and not 1. Next time through the loop, "i" = 2, and the corresponding "Forms(i).Caption" is "Form3", and so on.

Experiment with the "Debug" toolbar, especially the "Step Into" icon and the Quick Watch dialog.

Once you know how the Forms collection object works, it can be strategically placed to ensure all forms used within your VB application are indeed unloaded when it terminates.

By doing this, you need never worry about forms being left open, and consuming valuable memory on your PC.

More interesting facts about VB next month

Reprinted from the July 1998 issue of PC Update, the magazine of Melbourne PC User Group, Australia

[About Melbourne PC User Group]