SUMMARY
Visual Basic .NET or Visual Basic 2005 does not provide a built-in collection for
Form objects that are used in a project. This article demonstrates how to build a custom collection class that essentially supports the same functionality as the previous versions of Microsoft Visual Basic.
back to the top
Create the Custom Forms Collection
The first step to create a custom collection class in Visual Basic .NET or Visual Basic 2005 is to add a class module to the project. This class should inherit the
CollectionBase class and then shadow the
Add method to prevent objects other than
Form objects from being added to the collection. The following code illustrates how to add this class:
Public Class FormsCollection
Inherits CollectionBase
Public Shadows Function Add(ByVal FormObject As Form) As Form
MyBase.List.Add(FormObject)
Return FormObject
End Function
Public Shadows Sub Remove(ByVal FormObject as Form)
MyBase.List.Remove(FormObject)
End Sub
End Class
back to the top
Instantiate the Forms Collection Object
When you run the project, you must instantiate the custom collection class to add
Form objects to it. The easiest way to ensure that the collection is created before any forms are displayed is to set the startup object for the project to
Sub Main in the
Project Properties dialog box. Then, add a module to the project, and add the code to create the custom collection class and display the first form. The following code illustrates this:
Module StartHere
Public Forms As FormsCollection
Sub Main()
Forms = New FormsCollection()
Application.Run(New Form1())
End Sub
End Module
back to the top
Add and Remove Forms from the Collection
Lastly, you must modify the constructor for a form to add itself to the collection, and you must modify the destructor for a form to remove itself from the collection. The following code illustrates how to accomplish this:
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call.
Forms.Add(Me)
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
Forms.Remove(Me)
End Sub
Add the "Forms.Add(Me)" line to the constructor of any form that you want to place in the collection. Add the "Forms.Remove(Me)" line to the destructor of any form that you want to remove from the collection.
back to the top
Use the Forms Collection
You can now use the
Forms collection just as you would in previous versions of Visual Basic. The following example loops through the
Forms collection and displays the
Name property of any control on the form:
Dim LoopForm As Form
Dim LoopControl As Control
For Each LoopForm In Forms
For Each LoopControl In LoopForm.Controls
MessageBox.Show(LoopControl.Name)
Next
Next
back to the top
REFERENCES
For more information about Visual Basic 6.0
Forms collection, refer to the following Microsoft Web site:
For additional information, click the article number below
to view the article in the Microsoft Knowledge Base:
307210 How To Create Classes and Objects in Visual Basic .NET
back to the top