Share via


Layout 事件、OldLeft、OldTop、OldHeight、OldWidth 屬性範例

下列範例會使用 Layout事件內的 OldLeft 和 OldTop屬性以及OldHeight 和 OldWidth屬性,將控制項保留在其目前的位置和大小。

使用者按一下標示為[移動 ComboBox]CommandButton來移動控制項,然後回應訊息框。 使用者可以按一下標示為[重設 ComboBox] 的CommandButton,以重設另一個重複的控制項。

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

  • 兩個分別名為 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 支援與意見反應