Item 方法、OptionButton 控件示例

以下示例使用 Item 方法访问 控件页面 集合的单个成员。

用户为 Controls 集合或 MultiPage 选择选项按钮,然后单击 CommandButton。 相应控件的名称在 Label 中返回。

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

  • 一个名为"CommandButton1"的 CommandButton
  • 一个名为"Label1"的 Label
  • 两个名称分别为"OptionButton1"和"OptionButton2"的 OptionButton 控件。
  • 一个名为"MultiPage1"的 MultiPage
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 _ 
 .Item(MultiPage1.Value) 
 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

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。