Microsoft KB Archive/308537

= How to create a forms collection in Visual Basic .NET or in Visual Basic 2005 =

Article ID: 308537

Article Last Modified on 12/6/2006

-

APPLIES TO


 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic 2005

-



This article was previously published under Q308537





For a Microsoft Visual C# .NET version of this article, see 815707.

IN THIS TASK
SUMMARY
 * Create the Custom Forms Collection
 * Instantiate the Forms Collection Object
 * Add and Remove Forms from the Collection
 * Use the Forms Collection

REFERENCES



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 &quot;Forms.Add(Me)&quot; line to the constructor of any form that you want to place in the collection. Add the &quot;Forms.Remove(Me)&quot; 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