Поделиться через


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отображения. В следующем примере показано, как удалить элементы с помощью 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();
   }
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 получения индекса в ListBox.ObjectCollection пределах элемента, расположенного в настоящее время в верхней части видимой области элемента управления. Это свойство также можно использовать для размещения элемента в списке в верхней части видимой области элемента управления.

Применяется к