Layout 事件、LayoutEffect 属性、Move 方法示例Layout event, LayoutEffect property, Move method example

下面的示例使用**Move** 方法在窗体上移动选定的控件, 并使用**Layout** 事件和**LayoutEffect** 属性来标识移动的控件 (并更改了**用户窗体** 的布局)。The following example moves a selected control on a form with the Move method, and uses the Layout event and LayoutEffect property to identify the control that moved (and changed the layout of the UserForm).

用户单击要移动的控件, 然后单击**命令按钮**。The user clicks a control to move and then clicks the CommandButton. A message box displays the name of the control that is moving.A message box displays the name of the control that is moving.

若要使用此示例,请将此示例代码复制到窗体的"声明"部分。To use this example, copy this sample code to the Declarations portion of a form. 确保该窗体包含:Make sure that the form contains:

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 
 'Move the control, using Layout event when 
 'ToggleButton1.Value is True 
 ActiveControl.Move 0, 0, , , _ 
 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 
End Sub 
Private Sub ToggleButton1_Click() 
 If ToggleButton1.Value = True Then 
 ToggleButton1.Caption = "Use Layout Event" 
 ToggleButton1.Caption = "No Layout Event" 
 End If 
End Sub

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.