英語で読む

次の方法で共有


ListBox.TopIndex プロパティ

定義

ListBox に最初に表示される項目のインデックスを取得または設定します。

[System.ComponentModel.Browsable(false)]
public int TopIndex { get; set; }

プロパティ値

コントロールに最初に表示される項目の 0 から始まるインデックス番号。

属性

次のコード例では、 プロパティと プロパティTopIndexSelectedIndex使用して、現在選択されている項目を の表示領域の項目リストの先頭に移動する方法をListBox示します。 この例では、 クラスの メソッドを使用して項目を RemoveAt 削除する方法と、 メソッド System.Windows.Forms.ListBox.ObjectCollection を使用して ClearSelected すべての項目選択をクリアする方法をさらに示します。 コードは、最初に、 で現在選択されている項目を ListBox リストの先頭に移動します。 次に、現在選択されている項目の前にあるすべての項目が削除され、 内のすべての選択がクリアされます ListBox。 この例では、 ListBox 含まれているアイテムをフォームに追加し、 で ListBox項目が現在選択されている必要があります。

private void RemoveTopItems()
{
   // Determine if the currently selected item in the ListBox 
   // is the item displayed at the top in the ListBox.
   if (listBox1.TopIndex != listBox1.SelectedIndex)
      // Make the currently selected item the top item in the ListBox.
      listBox1.TopIndex = listBox1.SelectedIndex;

   // Remove all items before the top item in the ListBox.
   for (int x = (listBox1.SelectedIndex -1); x >= 0; x--)
   {
      listBox1.Items.RemoveAt(x);
   }

   // Clear all selections in the ListBox.
   listBox1.ClearSelected();
}

注釈

最初は、インデックス位置がゼロ (0) の項目は、 の可視領域の ListBox上部にあります。 の ListBox 内容がスクロールされている場合は、コントロールの表示領域の上部に別の項目がある可能性があります。 このプロパティを使用すると、コントロールの表示領域の上部に現在配置されている項目の の 内ListBox.ObjectCollectionListBoxのインデックスを取得できます。 また、このプロパティを使用して、リスト内の項目をコントロールの表示領域の上部に配置することもできます。

適用対象

製品 バージョン
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10