ListBox.ClearSelected Metoda

Definice

Zruší výběr všech položek v sadě ListBox.

public:
 void ClearSelected();
public void ClearSelected ();
member this.ClearSelected : unit -> unit
Public Sub ClearSelected ()

Příklady

Následující příklad kódu ukazuje, jak použít SelectedIndex vlastnost s TopIndex vlastností přesunout aktuálně vybranou položku do horní části seznamu položek v oblasti ListBoxzobrazení . Příklad dále ukazuje, jak odebrat položky pomocí RemoveAt metody System.Windows.Forms.ListBox.ObjectCollection třídy a jak vymazat všechny položky výběru pomocí ClearSelected metody. Kód nejprve přesune aktuálně vybranou položku v ListBox horní části seznamu. Kód pak odebere všechny položky před aktuálně vybranou položkou a vymaže všechny výběry v seznamu ListBox. Tento příklad vyžaduje, aby ListBox byly do formuláře přidány položky obsahující položky a že položka je aktuálně vybrána v sadě 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

Poznámky

Volání této metody je ekvivalentní nastavení SelectedIndex vlastnosti na záporné (-1). Tuto metodu můžete použít k rychlému zrušení výběru všech položek v seznamu.

Platí pro

Viz také