Compartir vía


Ejemplo de colección Controls y método Move

En el ejemplo siguiente se accede a controles individuales desde la colección Controls mediante un elemento For Each... Siguiente bucle. Cuando el usuario presiona CommandButton1, los demás controles se colocan en una columna a lo largo del borde izquierdo del formulario mediante el método Move .

Para usar este ejemplo, copie este código de muestra en la parte Declaraciones de un formulario. Asegúrese de que el formulario contiene un control CommandButton denominado CommandButton1 y otros controles.

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

Soporte técnico y comentarios

¿Tiene preguntas o comentarios sobre VBA para Office o esta documentación? Vea Soporte técnico y comentarios sobre VBA para Office para obtener ayuda sobre las formas en las que puede recibir soporte técnico y enviar comentarios.