Parent 属性示例

以下示例使用 Parent 属性来引用包含特定控件的控件或窗体。

若要使用此示例,请将此示例代码复制到窗体的 Declarations 部分。 确保该窗体包含:

  • 两个名称分别为"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 支持和反馈,获取有关如何接收支持和提供反馈的指南。