Controls Collection

Applies To

Frame control, MultiPage control, Page object.

Description

Includes all the controls contained in an object.

Remarks

Each control in the Controls collection of an object has a unique index whose value can be either an integer or a string. The index value for the first control in a collection is 0; the value for the second control is 1, and so on. This value indicates the order in which controls were added to the collection.

If the index is a string, it represents the name of the control. The Name property of a control also specifies a control's name.

You can use the Controls collection to enumerate or count individual controls, and to set their properties. For example, you can enumerate the Controls collection of a particular form and set the Height property of each control to a specified value.

Note The For Each...Next statement is useful for enumerating a collection.

Properties

Count property.

Methods

Add method, Clear method, Item method, Move method, Remove method.

See Also

Name property.

Example

The following example accesses individual controls from the Controls collection using a For Each...Next loop. When the user presses CommandButton1, the other controls are placed in a column along the left edge of the form using the Move method.

To use this example, copy this sample code to the Declarations portion of a form. Make sure that the form contains a CommandButton named CommandButton1 and several other controls.

Dim CtrlHeight As Single
Dim CtrlTop As Single
Dim CtrlGap As Single

Private Sub CommandButton1_Click()
    Dim MyControl As Control
    CtrlTop = 5

    For Each MyControl In Controls
        If MyControl.Name = "CommandButton1" Then
            'Don't move or resize this control.
        Else
            'Move method using named arguments
        MyControl.Move Top:=CtrlTop, Height:=CtrlHeight, Left:=5

        'Move method using unnamed arguments (left, top, width, height)
        'MyControl.Move 5, CtrlTop, ,CtrlHeight
            
            'Calculate top coordinate for next control
            CtrlTop = CtrlTop + CtrlHeight + CtrlGap
        End If
    Next

End Sub

Private Sub UserForm_Initialize()
    CtrlHeight = 20
    CtrlGap = 5
    
    CommandButton1.Caption = "Click to move controls"
    CommandButton1.AutoSize = True
    CommandButton1.Left = 120
    CommandButton1.Top = CtrlTop
End Sub
Example

See the Move method example.