Move Method

Applies To

CheckBox control, ComboBox control, CommandButton control, Controls collection, Frame control, Image control, Label control, ListBox control, MultiPage control, OptionButton control, ScrollBar control, SpinButton control, TabStrip control, TextBox control, ToggleButton control, UserForm object.

Description

Moves a form or control, or moves all the controls in the Controls collection.

Syntax

For a form or control:

object.Move( [Left [, Top [, Width [, Height [, Layout]]]]])

For the Controls collection:

object.Move( X, Y)

The Move method syntax has these parts:

Part

Description

object

Required. A valid object name.

Left

Optional. Single-precision value, in points, indicating the horizontal coordinate for the left edge of the object.

Top

Optional. Single-precision value, in points, that specifies the vertical coordinate for the top edge of the object.

Width

Optional. Single-precision value, in points, indicating the width of the object.

Height

Optional. Single-precision value, in points, indicating the height of the object.

Layout

Optional. A Boolean value indicating whether the Layout event is initiated for the control's parent following this move. False is the default value.

X, Y

Required. Single-precision value, in points, that specifies the change from the current horizontal and vertical position for each control in the Controls collection.


Settings

The maximum and minimum values for the Left, Top, Width, Height, X, and Y arguments vary from one application to another.

Remarks

For a form or control, you can move a selection to a specific location relative to the edges of the form that contains the selection.

You can use named arguments, or you can enter the arguments by position. If you use named arguments, you can list the arguments in any order. If not, you must enter the arguments in the order shown, using commas to indicate the relative position of arguments you do not specify. Any unspecified arguments remain unchanged.

For the Controls collection, you can move all the controls in this collection a specific distance from their current positions on a form, Frame, or Page.

Example

The following example demonstrates moving all the controls on a form by using the Move method with the Controls collection. The user clicks on the CommandButton to move the controls.

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.

Private Sub CommandButton1_Click()
    'Move each control on the form right 25 points and up 25 points.
Controls.Move 25, -25
End Sub
Example

The following example moves a selected control on a form with the Move method, and uses the Layout event and LayoutEffect property to identify the control that moved (and changed the layout of the UserForm). The user clicks a control to move and then clicks the CommandButton. A message box displays the name of the control that is moving.

To use this example, copy this sample code to the Declarations portion of a form. Make sure that the form contains:

  • A TextBox named TextBox1.
  • A ComboBox named ComboBox1.
  • An OptionButton named OptionButton1.
  • A CommandButton named CommandButton1.
  • A ToggleButton named ToggleButton1.
    Private Sub UserForm_Initialize()
        CommandButton1.Caption = "Move current control"
        CommandButton1.AutoSize = True
        CommandButton1.TakeFocusOnClick = False
        
        ToggleButton1.Caption = "Use Layout Event"
        ToggleButton1.Value = True
    End Sub
    
    Private Sub CommandButton1_Click()
        If ActiveControl.Name = "ToggleButton1" Then
            'Keep it stationary
        Else
            'Move the control, using Layout event when ToggleButton1.Value is True
            ActiveControl.Move 0, 0, , , ToggleButton1.Value
        End If
    End Sub
    
    Private Sub UserForm_Layout()
        Dim MyControl As Control
        
        MsgBox "In the Layout Event"
        
        'Find the control that is moving.
        For Each MyControl In Controls
            If MyControl.LayoutEffect = fmLayoutEffectInitiate Then
                MsgBox MyControl.Name & " is moving."
                Exit For
            End If
        Next
    End Sub
    
    Private Sub ToggleButton1_Click()
        If ToggleButton1.Value = True Then
            ToggleButton1.Caption = "Use Layout Event"
        Else
            ToggleButton1.Caption = "No Layout Event"
        End If
    End Sub