以下範例在 Layout 事件中使用 OldLeft 和 OldTop 屬性,以及 OldHeight 和 OldWidth 屬性,來保持控制項在目前的位置與大小。
使用者點擊標示為 Move ComboBox 的 CommandButton 來移動控制鍵,然後回覆訊息框。 使用者可以點擊標示為 Reset 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 支援與意見反應。