Layout 事件、OldLeft、OldTop、OldHeight 和 OldWidth 属性示例

以下示例使用 Layout 事件中的 OldLeft 和 OldTop 属性以及 OldHeight 和 OldWidth 属性将控件保留在其当前位置和大小。

用户单击标记为 Move ComboBoxCommandButton 移动控件,然后响应消息框。 用户可以单击标记为 Reset ComboBoxCommandButton 重置控件以进行另一次重复。

若要使用此示例,请将此示例代码复制到窗体的声明部分。 确保窗体包含以下内容:

  • 两个分别名为 CommandButton1 和 CommandButton2 的 CommandButton 控件。
  • 一个名为"ComboBox1"的 ComboBox
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, _ 
 ComboBox1.OldHeight 
 End If 
End Sub 
 
Private Sub CommandButton2_Click() 
 ComboBox1.Move ComboLeft, ComboTop, _ 
 ComboWidth, ComboHeight 
End Sub

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。