Upgrading the Forms Collection

The Forms collection in Visual Basic 6.0 is a collection of all loaded forms in the project. The most common uses of the Forms collection are to determine whether a form is loaded, to iterate through the loaded forms, and to unload a form by name. Visual Basic .NET does not support the Forms collection, but as with the other objects discussed in this chapter, it does provide similar functionality.

The following Visual Basic 6.0 example shows how to iterate through the Forms collection, determine whether a form called frmSecurity is open, and if so, disable the buttons btnInsert, btnUpdate, and btnDelete.

Public Sub checkForm()

Dim privateForm As Form Dim tempControl As Control For Each privateForm In Forms

If privateForm.Name = "frmSecurity" Then

For Each tempControl In privateForm.Controls If tempControl.Name = "btnInsert" Or _ tempControl.Name = "btnUpdate" Or _ tempControl.Name = "btnDelete" Then tempControl.Enabled = False End If

Next

MsgBox "For security reasons, this form can not be shown" End If

Next End Sub

The Forms collection cannot be automatically upgraded with the upgrade wizard because Visual Basic .NET does not support the Forms collection. As with the previous examples, attempting to use the upgrade wizard with code like the example will generate upgrade issue comments like the following.

' UPGRADE_ISSUE: Forms collection was not upgraded. For Each privateForm In Forms

The same approach for solving the Printers collection issues can also be applied to the Forms collection issues. Create a module and class masking the Visual Basic .NET functionality, as shown earlier in the Printers example. The code might look like the following.

Module FormsCollection

Public Forms As New FormsCo11ectionC1ass() End Module

Class FormsCollectionClass : Implements IEnumerable Private collec As New Co11ection() Sub Add(ByVa1 tempForm As Form)

collec.Add(tempForm) End Sub

Sub Remove(ByVa1 tempForm As Form) Dim itemCount As Integer For itemCount = l To collec.Count

If tempForm Is collec.Item(itemCount) Then collec.Remove(itemCount) Exit For End If Next End Sub

ReadOnly Property Item(ByVa1 index) As Form Get

Return collec.Item(index) End Get End Property

Overridable Function GetEnumerator() As _

IEnumerator Implements IEnumerable.GetEnumerator Return collec.GetEnumerator End Function End Class

The FormsCollectionClass will store all the loaded forms in a collection. Note that you need to make sure that each form is added to the collection when it is created and removed from the collection after it is released. For example, in the New event for frmSecurity, you can add it to the collection by adding the following line of code.

Forms.Add(Me)

Similarly, in the Disposed event, you can remove frmSecurity from the collection by adding the following line of code.

Forms.Remove(Me)

The Add and Remove lines must be added to the New and Disposed events for every form in the solution.

After the new module and class are added to the upgrade solution, the original checkForm subroutine code can be used without requiring any changes at all.

For Visual Basic 2005:

This solution will be obsolete in the new Visual Basic 2005, because the Forms collection is implemented in the My.Application.OpenForms class. This will make it possible for an automatic upgrade to be performed, with no need to create wrapper classes or modules with global members as shown in this example.

Upgraded Forms collection code will look very similar to the original code, as shown here.

Public Sub checkForm()

Dim privateForm As System.Windows.Forms.Form Dim tempControl As System.Windows.Forms.Control For Each privateForm In My.Application.OpenForms

If privateForm.Name = "frmSecurity" Then

For Each tempControl In privateForm.Controls If tempControl.Name = "btnInsert" Or _

tempControl.Name = "btnUpdate" Or tempControl.Name = "btnDelete"

Then tempControl.Enabled = False End If Next tempControl

MsgBox("For security this form can not be showed") End If Next privateForm End Sub

+1 0

Post a comment