ListBox.TopIndex 属性

定义

获取或设置 ListBox 中第一个可见项的索引。Gets or sets the index of the first visible item in the ListBox.

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

属性值

控件中第一个可见项的从零开始的索引。The zero-based index of the first visible item in the control.

属性

示例

下面的代码示例演示如何将 SelectedIndex 属性与 TopIndex 属性一起使用,以将当前选定项移到 ListBox显示区域中项列表的顶部。The following code example demonstrates how to use the SelectedIndex property with the TopIndex property to move the currently selected item to the top of the list of items in the display area of the ListBox. 该示例还演示了如何使用 System.Windows.Forms.ListBox.ObjectCollection 类的 RemoveAt 方法删除项,以及如何使用 ClearSelected 方法清除所有项选择。The example further demonstrates how to remove items using the RemoveAt method of the System.Windows.Forms.ListBox.ObjectCollection class, and how to clear all item selection using the ClearSelected method. 代码首先将 ListBox 中当前选定的项移动到列表的顶部。The code first moves the currently selected item in the ListBox to the top of the list. 然后,该代码移除当前选定项之前的所有项,并清除 ListBox中的所有选择。The code then removes all items before the currently selected item and clears all selections in the ListBox. 此示例要求将包含项的 ListBox 添加到窗体中,并且当前在 ListBox中选择了该项。This example requires that a ListBox containing items is added to a form and that an item is currently selected in the 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可见区域的顶部。Initially, the item with the index position zero (0) is at the top of the visible region of the ListBox. 如果 ListBox 的内容已滚动,则另一项可能位于控件的显示区域的顶部。If the contents of the ListBox have been scrolled, another item might be at the top of the control's display area. 您可以使用此属性来获取 ListBox.ObjectCollection 中当前位于控件可见区域顶部的项的 ListBox 的索引。You can use this property to obtain the index within the ListBox.ObjectCollection for the ListBox of the item that is currently positioned at the top of the visible region of the control. 还可以使用此属性将列表中的项定位到控件的可见区域的顶部。You can also use this property to position an item in the list at the top of the visible region of the control.

适用于