Wednesday, May 4, 2011

Forms Collection pada VB6

This section discusses the use of VB's Forms Collection. Topics covered include:
  • using methods and properties of the collection,
  • using specific items within the Forms Collection (referencing individual forms),
  • looping through the collection,
  • verifying loaded forms,
  • unloading forms from the collection.
In Visual Basic, a collection is just a group of objects. If you have more than one form in a project, you would have a Forms Collection that you can use to easily manipulate all forms or just one specific form.The Forms Collection is a built-in or intrinsic collection of VB. Another example of a collection built into VB is the Controls Collection that will return all the controls contained on the specified form.

The Forms Collection contains the loaded forms of all VB form types. MDI Parent forms, MDI Child forms, and non-MDI forms are all included in the collection, along with any forms created at runtime.

The Forms Collection only contains the forms that are currently loaded in memory. If a project contains multiple forms and only the Startup form has been loaded, the collection will only have one item. To be included in the Forms Collection, all forms must be loaded. They do not have to be shown onscreen, only loaded.

If a form is the unloaded from memory, that form is no longer part of the Forms Collection. The collection will reduce its count by one, and that form will no longer be available in the collection.


Methods and Properties of the Forms Collection

Most VB collections have various methods or properties that will return information regarding the collection of objects. The Forms Collection has only one property, Count. This property gives the total number of forms currently loaded by the application. To find the total number of forms in the project, just use code such as the following:

iFormCount = Forms.Count

To test the Forms.Count property in VB, use a single form and place one command button on the form. Open the code window for the command button and type the code shown in Listing 4.14.

PROGRAMMING WITH FORMS.COUNT PROPERTY

Sub Command1_Click()
Msgbox "The Forms Collection has " & _
Forms.Count & _
" forms currently", _
vbInformation, "Forms Count"
End Sub

This code will show a message box with a string that contains the number of forms in the collection. Remember that only loaded forms are in the collection. Any form that has not been loaded or that has been unloaded will not be part of the collection.

Using specific items within the Forms Collection

Each object in the collection can also be referred to by its specific ordinal number. This is an item number automatically assigned as the new items are added to the collection.

When referring to the ordinal number of a form, you must always remember that the Forms Collection is 0-based. The first item in the collection will always have the ordinal value of 0. The following lines demonstrate using the item value:

Forms(0).Caption
Forms(1).Name
Forms(2).Width

This code assumes that a VB project has three forms and that all forms have been loaded into memory. The first line will return the Caption of the very first item (item 0) in the collection. The second line will return the Name property of Form(1). The third line will return the value of the last form's width.

These code examples show how easy referencing specific items in the collection can be. This can be an alternative method to using the specific form names in a given project. With the Forms Collection, you also do not have to know the name of a given form to control it. This will assist in manipulating forms generated at runtime.

Looping Through the Forms Collection

Another way to use the Forms Collection is with various looping techniques. By using For...Next, For...Each, or Do...While statements, the programmer can loop through the collection to affect all the forms, search for specific forms, and search for specific properties or even values. This will assist in searching for information without having to program every form name individually.

A simple example would be to retrieve each form's Caption, as shown below.

LOOPING THROUGH THE FORMS COLLECTION WITH FOR...NEXT

Dim iLoop as Integer
For iLoop = 0 to Forms.Count - 1
MsgBox Forms(iLoop).Caption
Next iLoop

This code declares an integer variable for looping and then sets up the For...Next statement. Notice iLoop = 0. You must start at 0 because collections are 0-based. Because you will not always know the amount of forms in the collection, you can take the Forms.Count and subtract 1 from the total. You must remove 1 because the Count starts at 1 and the Collection starts at 0. When this sample code is
run, a message box displays with the Caption property of every form in the collection.

An alternative to the For... Next loop would be the For...Each loop, which does not depend on an integer counter (see Listing 4.16).

LOOPING THROUGH THE FORMS COLLECTION WITH FOR...EACH

Dim frmCurr as Form
For each frmCurr in Forms
MsgBox FrmCurr.Caption
Next frmCurr

This is probably preferable to the For...Next loop because you don't have to keep track of position in the collection.

Using the Forms Collection to Verify Whether a Form Is Loaded

When a form is referenced in code, it automatically loads the form into memory. An advantage of the Forms Collection is that it contains only forms that have been loaded into memory. This enables the programmer to test for a specific form by looping through the collection and testing the Name property of every item in the collection.

An example of this code is found in Listing program below.

VERIFYING WHETHER A PARTICULAR FORM IS LOADED

Sub Command1_Click()
Dim frmCurr as Form
For each frmCurr in Forms
If frmCurr.Name = "Form3" Then
MsgBox "Form3 has been loaded"
End If
Next frmCurr
End Sub

This code declares a form variable for looping and then sets up the For...Each statement. Inside the For...Each statement is an If test. With this code you are looking through each item in the collection and testing the property frmCurr.Name. This allows frmCurr to represent every form that is loaded in the collection. If the Name property is equal to Form3, a message box displays informing us that Form3 has been loaded into memory.

This code sample requires a project with at least three forms. The default form names are expected, and the Form_Load event is responsible for loading all forms into memory. Remember that if the forms are not loaded at the moment the code runs, they will not be included in the collection.

Using the Forms Collection to Unload All Forms

Another common use of the Forms Collection is to unload all forms. Although the End statement would provide a faster, simpler way to terminate the application, it can have unwanted effects because a call to End will immediately terminate the application without allowing any further events (such as Unload events) to run. Looping through the Forms Collection to unload all members might seem like a more complicated way to do the task, but it allows normal processing of Unload and other events.

The code of Listing 4.18 shows one way to control the loop. First, you must determine the total number of elements in the collection then reduce that number by one to accommodate for that fact that the collection is 0-based. The loop unloads collection members in reverse order from the highest-numbered member to the lowest. This is due to the fact that, after each unload of a collection member, the number of members in the collection decreases by one.

UNLOADING ALL FORMS WITH A DESCENDING FOR...NEXT LOOP

Sub cmdClose_Click()
Dim iLoop As Integer
Dim iHighestForm as integer
iHighestForm = Forms.Count — 1
For iLoop = iHighestForm To 0 Step — 1
Unload Forms(iLoop)
Next iLoop
End Sub

A more elegant way to unload all forms would be with a For...Each loop, as in the example of Listing program below.

UNLOADING ALL FORMS WITH A FOR...EACH LOOP

Sub cmdClose_Click()
Dim frmCurr as Form
For each frmCurr in Forms
Unload frmCurr
Next frmCurr
End Sub

Both of the above methods assume that at least one form has been loaded in the collection and that a command button named cmdClose is on one of the loaded forms. After run, all forms should be unloaded, and the project will terminate if no other forms are in memory.

No comments:

Post a Comment