共用方式為


Layout 事件、LayoutEffect 屬性、Move 方法範例

下列範例會使用 Move 方法在表單上移動選取的控制項,並使用 Layout 事件和 LayoutEffect 屬性來識別移動 (並變更 UserForm) 版面配置的控制項。

使用者按一下控制項來移動,然後按一下 CommandButton。 訊息方塊會顯示正在移動的控制項名稱。

若要使用本範例,請將此範例程式碼複製到表單的宣告部分中。 請確定該表單包含:

Private Sub UserForm_Initialize() 
 CommandButton1.Caption = "Move current control" 
 CommandButton1.AutoSize = True 
 CommandButton1.TakeFocusOnClick = False 
 
 ToggleButton1.Caption = "Use Layout Event" 
 ToggleButton1.Value = True 
End Sub 
 
Private Sub CommandButton1_Click() 
 If ActiveControl.Name = "ToggleButton1" Then 
 'Keep it stationary 
 Else 
 'Move the control, using Layout event when 
 'ToggleButton1.Value is True 
 ActiveControl.Move 0, 0, , , _ 
 ToggleButton1.Value 
 End If 
End Sub 
 
Private Sub UserForm_Layout() 
 Dim MyControl As Control 
 
 MsgBox "In the Layout Event" 
 
 'Find the control that is moving. 
 For Each MyControl In Controls 
 If MyControl.LayoutEffect = _ 
 fmLayoutEffectInitiate Then 
 MsgBox MyControl.Name & " is moving." 
 Exit For 
 End If 
 Next 
End Sub 
 
Private Sub ToggleButton1_Click() 
 If ToggleButton1.Value = True Then 
 ToggleButton1.Caption = "Use Layout Event" 
 Else 
 ToggleButton1.Caption = "No Layout Event" 
 End If 
End Sub

支援和意見反應

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