Condividi tramite


ListBox.ClearSelected Metodo

Definizione

Deseleziona tutti gli elementi nell'oggetto ListBox.

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

Esempio

Nell'esempio di codice seguente viene illustrato come utilizzare la proprietà con la SelectedIndexTopIndex proprietà per spostare l'elemento attualmente selezionato all'inizio dell'elenco di elementi nell'area di visualizzazione dell'oggetto ListBox. Nell'esempio viene illustrato ulteriormente come rimuovere elementi usando il RemoveAt metodo della System.Windows.Forms.ListBox.ObjectCollection classe e come cancellare la selezione di tutti gli elementi usando il ClearSelected metodo . Il codice sposta innanzitutto l'elemento attualmente selezionato nell'oggetto ListBox all'inizio dell'elenco. Il codice rimuove quindi tutti gli elementi prima dell'elemento attualmente selezionato e cancella tutte le selezioni nell'oggetto ListBox. In questo esempio è necessario che un ListBox elemento contenitore venga aggiunto a un modulo e che un elemento sia attualmente selezionato in 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

Commenti

La chiamata a questo metodo equivale all'impostazione della SelectedIndex proprietà su uno negativo (-1). È possibile utilizzare questo metodo per deselezionare rapidamente tutti gli elementi nell'elenco.

Si applica a

Vedi anche