Taal

ListBox.TopIndex Eigenschap

Definitie

Hiermee haalt of stelt u de index van het eerste zichtbare item in de 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

Waarde van eigenschap

De op nul gebaseerde index van het eerste zichtbare item in het besturingselement.

Kenmerken

Voorbeelden

In het volgende codevoorbeeld ziet u hoe u de SelectedIndex eigenschap gebruikt met de TopIndex eigenschap om het geselecteerde item naar de bovenkant van de lijst met items in het weergavegebied van het ListBoxitem te verplaatsen. In het voorbeeld ziet u verder hoe u items verwijdert met behulp van de methode RemoveAt van de klasse System.Windows.Forms.ListBox.ObjectCollection en hoe u alle itemselectie kunt wissen met behulp van de methode ClearSelected. Met de code wordt eerst het geselecteerde item boven aan ListBox de lijst verplaatst. De code verwijdert vervolgens alle items vóór het geselecteerde item en wist alle selecties in de ListBox. In dit voorbeeld moet een ListBox item met items worden toegevoegd aan een formulier en dat er momenteel een item is geselecteerd in het ListBoxformulier.

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

Opmerkingen

In eerste instantie bevindt het item met de indexpositie nul (0) zich boven aan het zichtbare gebied van de ListBox. Als de inhoud van het ListBox besturingselement is geschoven, bevindt een ander item zich mogelijk boven aan het weergavegebied van het besturingselement. U kunt deze eigenschap gebruiken om de index te verkrijgen binnen het ListBox.ObjectCollection voor het ListBox item dat zich momenteel boven aan het zichtbare gebied van het besturingselement bevindt. U kunt deze eigenschap ook gebruiken om een item in de lijst boven aan het zichtbare gebied van het besturingselement te plaatsen.

Van toepassing op