ListBox.ClearSelected Metodo

Definizione

Deseleziona tutti gli elementi nel controllo ListBox.

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

Esempio

Nell'esempio di codice seguente viene illustrato come usare la proprietà con la SelectedIndex TopIndex proprietà per spostare l'elemento attualmente selezionato nella parte superiore dell'elenco di elementi nell'area di visualizzazione dell'oggetto ListBox. L'esempio illustra ulteriormente come rimuovere gli 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 prima l'elemento attualmente selezionato nella ListBox parte superiore 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 contenente venga aggiunto a un modulo e che un elemento sia attualmente selezionato nell'oggetto 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 a impostare la SelectedIndex proprietà su una (-1). È possibile usare questo metodo per deselezionare rapidamente tutti gli elementi nell'elenco.

Si applica a

Vedi anche