Share via


Item 方法、OptionButton 控制項範例

下列範例會使用 Item 方法來存取 ControlsPages 集合的個別成員。

使用者選擇 Controls 集合MultiPage的選項按鈕,然後按一下 CommandButton。 適當的控制項名稱會在 標籤中傳回。

若要使用本範例,請將此範例程式碼複製到表單的宣告部分中。 請確定表單包含:

  • 一個名為 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 支援與意見反應