Using the Forms Collection
The forms collection in Visual Basic 6 is a collection of all open forms in the project. The most common uses of the forms collection are to determine whether a form is open, to iterate through the forms collection, and to close a form by name. The following Visual Basic 6 example shows how to iterate through the forms collection, determine whether a form called Form1 is open, and then unload Form1 if it is open:
Dim f As Form For Each f In Forms If f.Name = "Form1" Then Unload f MsgBox "Form unloaded" End If Next
The forms collection cannot be upgraded automatically, so if the previous code is upgraded, the collection is marked with an upgrade issue:
'UPGRADE_ISSUE: Forms collection was not upgraded. For Each f In Forms
The only way to achieve the same effect in Visual Basic .NET is to implement your own forms collection. Luckily, this is easier than it sounds. First create a new Visual Basic .NET Windows application. Add a new module, and in the module put the following code (you can find this code on the companion CD in the file FormCollectionClass.vb):
Module FormsCollection Public Forms As New FormsCollectionClass() End Module Class FormsCollectionClass : Implements IEnumerable Private c As New Collection() Sub Add(ByVal f As Form) c.Add(f) End Sub Sub Remove(ByVal f As Form) Dim itemCount As Integer For itemCount = 1 To c.Count If f Is c.Item(itemCount) Then c.Remove(itemCount) Exit For End If Next End Sub ReadOnly Property Item(ByVal index) As Form Get Return c.Item(index) End Get End Property Overridable Function GetEnumerator() As _ IEnumerator Implements IEnumerable.GetEnumerator Return c.GetEnumerator End Function End Class
This is the collection that will maintain your collection of forms. You need to make sure that each form is added to the collection when it is created and removed from the collection when it is disposed of. In the New event for Form1, insert the following line:
Forms.Add(Me)
In the Disposed event, insert this line:
Forms.Remove(Me)
That s all you need to do to implement your forms collection. If your application has many forms, you ll have to put the New and Dispose code in every one.
Let s see how to use this collection in code. The following Visual Basic .NET example does exactly what the first Visual Basic 6 example in this chapter did: loop through the forms collection, determine whether a form called Form1 is open, and unload it if it is open.
Dim f As Form For Each f In Forms If f.Name = "Form1" Then f.Close() End If Next
You can use this forms collection to unload forms, loop through the collection of open forms, or simply check whether a particular form is open.
Do You Really Need a Forms Collection?
Before adding a forms collection to your project, you may want to ask yourself whether you really need it. One drawback is that you need to remember to add the code to every form in your application. If you forget to add it to a single form, that form will not be added to the collection. If you do use the forms collection, and you use it a lot, consider adding the New and Dispose code to the template from which every new form is created.
Some applications created with the Visual Basic Application Wizard contain code that loops through the forms collection and closes every form in the application. In many cases you can safely remove this code from the project and avoid having to create your own forms collection.