Parent 属性示例Parent property example

下面的示例使用**Parent** 属性来引用包含特定控件的控件或窗体。The following example uses the Parent property to refer to the control or form that contains a specific control.

若要使用此示例,请将此示例代码复制到窗体的 Declarations 部分。To use this example, copy this sample code to the Declarations portion of a form. 确保该窗体包含:Make sure that the form contains:

  • 两个名称分别为"Label1"和"Label2"的 Label 控件。Two Label controls named Label1 and Label2.
  • 一个名为"CommandButton1"的 CommandButtonA CommandButton named CommandButton1.
  • 一个或多个可选的其他控件。One or more additional controls of your choice.
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

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.