Wednesday, May 4, 2011

Controls Collection in VB6

The Controls Collection is a built-in collection belonging to a form and is maintained by Visual Basic. The Controls Collection contains references to all the controls on that form. Each member of the collection points to a different object on that form—for example, a TextBox, ComboBox, ListView, and so on.

Each form in a project has its own Controls Collection that you can use to navigate through the different controls on a form. You can refer to each control within the collection as follows:

Form.Controls(index)

where Form is the form name, and Index is an integer value referring to the desired control. Since the Controls Collection is zero-based, Index values range from 0 up through a number that is one less than the total number of controls on the form. If you have 10 controls on a form, the Index values for the Controls Collection would range from 0 through 9.

You will usually use in a Controls Collection when you need to process all the controls or the majority of the controls on a given form. You can loop through a Controls Collection either with a For Next loop, as in Prog 1, or with a For Each loop, as inProg 2.

Prog 1
LOOPING THROUGH A CONTROLS COLLECTION WITH A FOR NEXT LOOP
For I = 0 To Form1.Controls.Count - 1
' process Form1.Controls(I)
Next

Prog 2
LOOPING THROUGH A CONTROLS COLLECTION WITH A
FOR EACH LOOP
For Each obj In Form1. Controls
' process obj
Next

A common use of the Controls Collection is to clear all the controls on a form. If the user wants to reset all the fields on a data entry form, you can code a Clear button with code such as that found in Prog 3

Prog 3
CODE TO RESET ALL FIELDS ON A DATA ENTRY FORM

Private Sub cmdClear_Click()
Dim objControl As Control
Dim sTemp As String
For Each objControl In Me.Controls
If TypeOf objControl Is TextBox Then
' clear the text
objControl.Text = ""
ElseIf TypeOf objControl Is ComboBox Then
' reset the listindex
objControl.ListIndex = -1
ElseIf TypeOf objControl Is Label Then
' leave labels as is
ElseIf TypeOf objControl Is CommandButton Then
' leave commandbuttons as is
ElseIf TypeOf objControl Is MaskEdBox Then
' clear the masked edit control
sTemp = objControl.Mask
objControl.Mask = ""
objControl.Text = ""
objControl.Mask = sTemp
Else
' leave any other control alone
End If
Next
End Sub

This code in the Listing loops through all the controls on a form and clears each control based on its type. To clear a TextBox control, for example, you would set the Text property to a 0-length string. To clear a CheckBox, you would set the value property to vbUnchecked.

Notice that the code in the Listing uses an If statement to determine the type of control being cleared. You must make sure you know the type of control you reference to avoid runtime errors. Errors occur if you try to reference a property of a control that is not a valid property. If the first control on a form is a TextBox, you can clear that control by using:

Me.Controls(0).Text = ""

If, on the other hand, the first control is a CheckBox that does not have a Text property, with the same code, you will get a runtime error from Visual Basic. The Clear button code example avoids this problem by checking for specific control types within the Controls Collection. The syntax for the If statement is as follows:

If TypeOf objectname Is objecttype Then
' code
EndIf

where objectname is the object or control (that is TextBox, ComboBox, and so forth) on which you are working and objecttype is the class of the object. Using this If structure before you reference a control through the Controls Collection will help you avoid runtime errors.

No comments:

Post a Comment