Controls 集合、Move 方法示例

以下示例使用 For Each 访问 Controls 集合中的单个控件 ...下一个 循环。 当用户按下 CommandButton1 时,使用 Move 方法将其他控件放置在窗体左边缘的列中。

若要使用此示例,请将此示例代码复制到窗体的 Declarations 部分。 确保窗体包含名为 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 支持和反馈,获取有关如何接收支持和提供反馈的指南。