Item 方法、OptionButton 控件示例Item method, OptionButton control example

下面的示例使用**Item** 方法访问**Controls** 和**Pages** 集合的各个成员。The following example uses the Item method to access individual members of the Controls and Pages collections.

用户选择控件集合或**多页****控件的选项按钮, 然后单击命令** 按钮。The user chooses an option button for either the Controls collection or the MultiPage, and then clicks the CommandButton. 在**标签** 中返回相应控件的名称。The name of the appropriate control is returned in the Label.

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

  • 一个名为"CommandButton1"的 CommandButtonA CommandButton named CommandButton1.
  • 一个名为"Label1"的 LabelA Label named Label1.
  • 两个名称分别为"OptionButton1"和"OptionButton2"的 OptionButton 控件。Two OptionButton controls named OptionButton1 and OptionButton2.
  • 一个名为"MultiPage1"的 MultiPageA MultiPage named MultiPage1.
Dim MyControl As Object 
Dim ControlsIndex As Integer 
Private Sub CommandButton1_Click() 
 If OptionButton1.Value = True Then 
 'Process Controls collection for UserForm 
 Set MyControl = Controls.Item(ControlsIndex) 
 Label1.Caption = MyControl.Name 
 'Prepare index for next control on Userform 
 ControlsIndex = ControlsIndex + 1 
 If ControlsIndex >= Controls.Count Then 
 ControlsIndex = 0 
 End If 
 ElseIf OptionButton2.Value = True Then 
 'Process Current Page of Pages collection 
 Set MyControl = MultiPage1.Pages _ 
 Label1.Caption = MyControl.Name 
 End If 
End Sub 
Private Sub UserForm_Initialize() 
 ControlsIndex = 0 
 OptionButton1.Caption = "Controls Collection" 
 OptionButton2.Caption = "Pages Collection" 
 OptionButton1.Value = True 
 CommandButton1.Caption = "Get Member Name" 
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.