Freigeben über


ListBox.TopIndex-Eigenschaft

Ruft den Index des ersten sichtbaren Elements in der ListBox ab oder legt diesen fest.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Syntax

'Declaration
Public Property TopIndex As Integer
'Usage
Dim instance As ListBox
Dim value As Integer

value = instance.TopIndex

instance.TopIndex = value
public int TopIndex { get; set; }
public:
property int TopIndex {
    int get ();
    void set (int value);
}
/** @property */
public int get_TopIndex ()

/** @property */
public void set_TopIndex (int value)
public function get TopIndex () : int

public function set TopIndex (value : int)

Eigenschaftenwert

Der nullbasierte Index des ersten sichtbaren Elements im Steuerelement.

Hinweise

Anfänglich befindet sich das Element mit der Indexposition 0 (null) am oberen Rand des sichtbaren Bereichs der ListBox. Nachdem ein Bildlauf durch den Inhalt der ListBox durchgeführt wurde, kann sich am oberen Rand des Anzeigebereichs des Steuerelements ein anderes Element befinden. Mit dieser Eigenschaft können Sie den Index in der ListBox.ObjectCollection für die ListBox des Elements abrufen, das sich derzeit am oberen Rand des sichtbaren Bereichs des Steuerelements befindet. Sie können mit dieser Eigenschaft auch ein Element in der Liste am oberen Rand des sichtbaren Bereichs des Steuerelements positionieren.

Beispiel

Im folgenden Codebeispiel wird veranschaulicht, wie mit der SelectedIndex-Eigenschaft und der TopIndex-Eigenschaft das aktuell ausgewählte Element an die obere Position der Elementliste im Anzeigebereich der ListBox verschoben wird. Darüber hinaus wird veranschaulicht, wie Elemente mithilfe der RemoveAt-Methode der System.Windows.Forms.ListBox.ObjectCollection-Klasse entfernt werden und wie die gesamte Elementauswahl mithilfe der ClearSelected-Methode aufgehoben wird. Im Code wird zunächst das derzeit ausgewählte Element in der ListBox an die erste Stelle in der Liste verschoben. Anschließend werden alle Elemente vor dem derzeit ausgewählten Element entfernt, und die gesamte Auswahl in der ListBox wird aufgehoben. Für dieses Beispiel muss einem Formular eine ListBox mit Elementen hinzugefügt werden, und in der ListBox muss gegenwärtig ein Element ausgewählt sein.

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 'RemoveTopItems
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 void RemoveTopItems()
{
    // Determine if the currently selected item in the ListBox 
    // is the item displayed at the top in the ListBox.
    if (listBox1.get_TopIndex() != listBox1.get_SelectedIndex()) {
        // Make the currently selected item the top item in the ListBox.
        listBox1.set_TopIndex(listBox1.get_SelectedIndex());
    }
    // Remove all items before the top item in the ListBox.
    for (int x = listBox1.get_SelectedIndex() - 1; x >= 0; x--) {
        listBox1.get_Items().RemoveAt(x);
    }
    // Clear all selections in the ListBox.
    listBox1.ClearSelected();
} //RemoveTopItems

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

ListBox-Klasse
ListBox-Member
System.Windows.Forms-Namespace