Layout 事件、OldLeft、OldTop、OldHeight 和 OldWidth 属性示例Layout event, OldLeft, OldTop, OldHeight, OldWidth properties example

下面的示例使用**Layout** 事件中的**OldLeft 和 OldTop** 属性以及**OldHeight 和 OldWidth** 属性将控件保持其当前位置和大小。The following example uses the OldLeft and OldTop properties and OldHeight and OldWidth properties within the Layout event to keep a control at its current position and size.

用户单击标记为Move ComboBox的**命令按钮** 移动控件, 然后响应消息框。The user clicks the CommandButton labeled Move ComboBox to move the control, and then responds to a message box. 用户可以单击标记为 Reset ComboBoxCommandButton 重置控件以进行另一次重复。The user can click the CommandButton labeled Reset ComboBox to reset the control for another repetition.

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

  • 两个分别名为 CommandButton1 和 CommandButton2 的 CommandButton 控件。Two CommandButton controls named CommandButton1 and CommandButton2.
  • 一个名为"ComboBox1"的 ComboBoxA ComboBox named ComboBox1.
Dim Initialize As Integer 
Dim ComboLeft, ComboTop, ComboWidth, _ 
 ComboHeight As Integer 
Private Sub UserForm_Initialize() 
 Initialize = 0 
 CommandButton1.Caption = "Move ComboBox" 
 CommandButton2.Caption = "Reset ComboBox" 
 'Information for resetting ComboBox 
 ComboLeft = ComboBox1.Left 
 ComboTop = ComboBox1.Top 
 ComboWidth = ComboBox1.Width 
 ComboHeight = ComboBox1.Height 
End Sub 
Private Sub CommandButton1_Click() 
 ComboBox1.Move 0, 0, , , True 
End Sub 
Private Sub UserForm_Layout() 
 Dim MyControl As Control 
 Dim MsgBoxResult As Integer 
 'Suppress MsgBox on initial layout event. 
 If Initialize = 0 Then 
 Initialize = 1 
 Exit Sub 
 End If 
 MsgBoxResult = MsgBox("In Layout event " _ 
 & "- Continue move?", vbYesNo) 
 If MsgBoxResult = vbNo Then 
 ComboBox1.Move ComboBox1.OldLeft, _ 
 ComboBox1.OldTop, ComboBox1.OldWidth, _ 
 End If 
End Sub 
Private Sub CommandButton2_Click() 
 ComboBox1.Move ComboLeft, ComboTop, _ 
 ComboWidth, ComboHeight 
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.