Layout Event

Applies To

Frame control, MultiPage control, UserForm object.

Description

Occurs when a form, Frame, or MultiPage changes size.

Syntax

For MultiPage

Private Sub object_Layout( index As Long)

For all other controls

Private Sub object_Layout( )

The Layout event syntax has these parts:

Part

Description

object

Required. A valid object.

index

Required. The index of the page in a MultiPage that changed size.


Remarks

The default action of the Layout event is to calculate new positions of controls and to repaint the screen.

A user can initiate the Layout event by changing the size of a control.

For controls that support the AutoSize property, the Layout event is initiated when AutoSize changes the size of the control. This occurs when the user changes the value of a property that affects the size of a control. For example, increasing the Font size of a TextBox or Label can significantly change the dimensions of the control and initiate a Layout event.

See Also

AutoSize property, LayoutEffect property, OldHeight, OldWidth properties, OldLeft, OldTop properties.

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