以下範例透過使用「For Each...」來存取控制項集合中的個別控制項。下一圈。 當使用者按下 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 支援與意見反應。