Forms Collection 
Author Message
 Forms Collection

There must be a good reason why vb.net does not provide a forms collection?
Why have they got rid of it?
What is now the proper way to iterate through forms for example?


Mon, 31 Jan 2005 00:40:57 GMT  
 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.



Mon, 31 Jan 2005 11:48:11 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Forms Collection: Using it to Open Forms by Passing the Form Name as a String Variable

2. Sub forms not in the Forms collection?

3. 3rd party generated forms not in Forms collection

4. Hiding forms not in Forms collection

5. Getting to the Forms collection from a form reference

6. Subforms not in the Forms collection?

7. forms collection

8. Syntax question for Forms collection

9. Forms Collection and Load

10. Convert VB6 Forms collection

11. Forms Collection

12. Forms collection...

 

 
Powered by phpBB® Forum Software