Verwenden der Controls-Auflistung zum Verschieben von Steuerelementen auf einer Seite

The following example accesses individual controls from the Microsoft Forms 2.0 Controls collection using a For Each...Next loop. Wenn der Benutzer CommandButton1 drückt, werden die anderen Steuerelemente mithilfe der Move-Methode des Steuerelements am linken Rand des Formulars in einer Spalte platziert.

Kopieren Sie diesen Beispielcode in den Skript-Editor eines Formulars. To run the code you need to open the form so the Open event of the item will activate. Make sure that the form contains a CommandButton named CommandButton1 and several other controls.

Dim CtrlHeight 
Dim CtrlTop 
Dim CtrlGap 
Dim CommandButton1 
 
Sub Item_Open() 
 Set CommandButton1 = Item.GetInspector.ModifiedFormPages("P.2").CommandButton1 
 
 CtrlHeight = 20 
 CtrlGap = 5 
 
 CommandButton1.Caption = "Click to move controls" 
 CommandButton1.AutoSize = True 
 CommandButton1.Left = 120 
 CommandButton1.Top = CtrlTop 
End Sub 
 
Sub CommandButton1_Click() 
 Dim MyControl 
 
 Set AllControls = Item.GetInspector.ModifiedFormPages("P.2").Controls 
 
 CtrlTop = 5 
 
 For i = 0 to AllControls.Count - 1 
 Set MyControl = AllControls(i) 
 If MyControl.Name = "CommandButton1" Then 
 'Don't move or resize this control. 
 Else 
 '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

Support und Feedback

Haben Sie Fragen oder Feedback zu Office VBA oder zu dieser Dokumentation? Unter Office VBA-Support und Feedback finden Sie Hilfestellung zu den Möglichkeiten, wie Sie Support erhalten und Feedback abgeben können.