Freigeben über


ListBox.ClearSelected-Methode

Hebt die Auswahl sämtlicher Elemente in der ListBox auf.

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

Syntax

'Declaration
Public Sub ClearSelected
'Usage
Dim instance As ListBox

instance.ClearSelected
public void ClearSelected ()
public:
void ClearSelected ()
public void ClearSelected ()
public function ClearSelected ()

Hinweise

Das Aufrufen dieser Methode entspricht dem Festlegen der SelectedIndex-Eigenschaft auf -1. Mit dieser Methode können Sie die Auswahl aller Elemente in der Liste schnell aufheben.

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 von 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. In diesem 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

Siehe auch

Referenz

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