Share via


Controls 集合、Move 方法範例

下列範例會使用 For Each... 從 Controls 集合存取個別控 件。下一個 迴圈。 當使用者按下 CommandButton1 時,其他控制項會使用 Move 方法放在表單左邊緣的資料行中。

若要使用本範例,請將此範例程式碼複製到表單的宣告部分中。 請確定表單包含名為 CommandButton1 的 CommandButton 和其他數個控制項。

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

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應