ListBox.TopIndex 屬性

定義

取得或設定 ListBox 中第一個可見項目的索引。

public:
 property int TopIndex { int get(); void set(int value); };
[System.ComponentModel.Browsable(false)]
public int TopIndex { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.TopIndex : int with get, set
Public Property TopIndex As Integer

屬性值

控制項中第一個可見項目之以零為起始的索引。

屬性

範例

下列程式碼範例示範如何使用 SelectedIndex 屬性搭配 TopIndex 屬性,將目前選取的專案移至 顯示區域中 ListBox 專案清單頂端。 此範例會進一步示範如何使用 類別的 System.Windows.Forms.ListBox.ObjectCollection 方法移除專案 RemoveAt ,以及如何使用 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();
   }
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();
}
Private Sub 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 Then
      ' Make the currently selected item the top item in the ListBox.
      listBox1.TopIndex = listBox1.SelectedIndex
   End If
   ' Remove all items before the top item in the ListBox.
   Dim x As Integer
   For x = listBox1.SelectedIndex - 1 To 0 Step -1
      listBox1.Items.RemoveAt(x)
   Next x

   ' Clear all selections in the ListBox.
   listBox1.ClearSelected()
End Sub

備註

一開始,索引位置為零的專案 (0) 位於 的可見區域 ListBox 頂端。 如果 的內容 ListBox 已經捲動,另一個專案可能位於控制項的顯示區域頂端。 您可以使用這個屬性,取得目前位於控制項可見區域頂端之 專案的 中的 索引 ListBox.ObjectCollectionListBox 。 您也可以使用這個屬性,將專案放在控制項可見區域頂端的清單中。

適用於