Parent 屬性範例
下列範例會使用 Parent 屬性來參照包含特定控制項的控制項或表單。
若要使用本範例,請將此範例程式碼複製到表單的宣告部分中。 請確定該表單包含:
- 兩個分別名為 Label1 和 Label2 的 Label 控制項。
- 一個名為 CommandButton1 的 CommandButton 。
- 一個或多個選擇性的其他控制項。
Dim MyControl As Object
Dim MyParent As Object
Dim ControlsIndex As Integer
Private Sub UserForm_Initialize()
ControlsIndex = 0
CommandButton1.Caption = "Get Control and Parent"
CommandButton1.AutoSize = True
CommandButton1.WordWrap = True
End Sub
Private Sub CommandButton1_Click()
'Process Controls collection for UserForm
Set MyControl = Controls.Item(ControlsIndex)
Set MyParent = MyControl.Parent
Label1.Caption = MyControl.Name
Label2.Caption = MyParent.Name
'Prepare index for next control on Userform
ControlsIndex = ControlsIndex + 1
If ControlsIndex >= Controls.Count Then
ControlsIndex = 0
End If
End Sub
支援和意見反應
有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應。