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

Looping Through the Forms Collection in Visual Basic 6 (VB6)

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 in Listing 4.15.

LISTING 4.15
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).

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 topics

See Also


  

<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © Freetutes.com | All Rights Reserved