Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleUse a collection to display one instance of a form given its name
Keywordsform, form name, show form
CategoriesControls, Software Engineering
 
Store the form objects in the collection using their names as keys. Use the collection to display the forms when needed.

This has the advantage that the forms are placed in the collection when the program starts so accessing the forms later is fast.

One disadvantage is that this method only allows the program to display one instance of each form.

 
Private FormObjects As New Collection

' Load the FormObjects collection with form
' objects using their names as keys.
Private Sub Form_Load()
    FormObjects.Add Form2, "Form2"
    FormObjects.Add Form3, "Form3"
End Sub

' Display the named form.
Private Sub cmdShowForm_Click()
Dim frm As Form

    On Error Resume Next
    Set frm = FormObjects(txtFormName.Text)
    If Err.Number Then
        MsgBox "Unknown form name"
        Exit Sub
    End If
    On Error GoTo 0
    
    frm.Show
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated