TopIndex プロパティの例

次の例では、ListBox に表示される上位の項目と、 ListBox 内にフォーカスがある項目を 識別します。 次の使用例は、 TopIndex プロパティを使用して 、ListBox の上部に表示される項目を識別し、 ListIndex プロパティを使用してフォーカスを持つアイテムを識別します。

リスト ボックス (ListBox) の中から項目を選択します。 TopIndexListIndex の表示値は、ユーザーが項目を選択したとき、またはユーザーが CommandButton をクリックしたときに更新されます。

この例を使用するには、以下のサンプル コードをフォームの宣言部分にコピーします。 Make sure that the form contains:

  • ラベル ( Label ) コントロール (Label1)
  • テキスト ボックス ( TextBox ) コントロール (TextBox1)
  • ラベル ( Label ) コントロール (Label2)
  • TextBox2 と命名された TextBox
  • CommandButton1 という名前の CommandButton
  • ListBox1 という名前の ListBox
Private Sub CommandButton1_Click() 
 ListBox1.TopIndex = ListBox1.ListIndex 
 TextBox1.Text = ListBox1.TopIndex 
 TextBox2.Text = ListBox1.ListIndex 
End Sub 
 
Private Sub ListBox1_Change() 
 TextBox1.Text = ListBox1.TopIndex 
 TextBox2.Text = ListBox1.ListIndex 
End Sub 
 
Private Sub UserForm_Initialize() 
 Dim i As Integer 
 
 For i = 0 To 24 
 ListBox1.AddItem "Choice " & (i + 1) 
 Next i 
 ListBox1.Height = 66 
 CommandButton1.Caption = "Move to top of list" 
 CommandButton1.AutoSize = True 
 CommandButton1.TakeFocusOnClick = False 
 
 Label1.Caption = "Index of top item" 
 TextBox1.Text = ListBox1.TopIndex 
 
 Label2. Caption = "Index of current item" 
 Label2.AutoSize = True 
 Label2.WordWrap = False 
 TextBox2.Text = ListBox1.ListIndex 
End Sub

サポートとフィードバック

Office VBA またはこの説明書に関するご質問やフィードバックがありますか? サポートの受け方およびフィードバックをお寄せいただく方法のガイダンスについては、Office VBA のサポートおよびフィードバックを参照してください。