ListBox.TopIndex Właściwość

Definicja

Pobiera lub ustawia indeks pierwszego widocznego elementu w elemencie 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

Wartość właściwości

Int32

Indeks zerowy pierwszego widocznego elementu w kontrolce.

Atrybuty

Przykłady

Poniższy przykład kodu przedstawia sposób użycia SelectedIndex właściwości z właściwością TopIndex w celu przeniesienia aktualnie wybranego elementu na górę listy elementów w obszarze wyświetlania ListBoxobiektu . W tym przykładzie pokazano, jak usuwać elementy przy użyciu RemoveAt metody System.Windows.Forms.ListBox.ObjectCollection klasy oraz jak wyczyścić zaznaczenie wszystkich elementów przy użyciu ClearSelected metody . Kod najpierw przenosi aktualnie wybrany element w ListBox górnej części listy. Następnie kod usuwa wszystkie elementy przed aktualnie zaznaczonym elementem i czyści wszystkie wybory w elemencie ListBox. W tym przykładzie do ListBox formularza jest dodawany element zawierający, a element jest obecnie zaznaczony w elemencie 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

Uwagi

Początkowo element o pozycji indeksu zero (0) znajduje się u góry widocznego regionu .ListBox Jeśli zawartość obiektu ListBox została przewinięta, w górnej części obszaru wyświetlania kontrolki może znajdować się inny element. Tej właściwości można użyć do uzyskania indeksu dla ListBox.ObjectCollection ListBox elementu, który jest obecnie umieszczony w górnej części widocznego regionu kontrolki. Za pomocą tej właściwości można również umieścić element na liście w górnej części widocznego regionu kontrolki.

Dotyczy