ListBox.ClearSelected Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Désélectionne tous les éléments dans le ListBox.
public:
void ClearSelected();
public void ClearSelected();
member this.ClearSelected : unit -> unit
Public Sub ClearSelected ()
Exemples
L’exemple de code suivant montre comment utiliser la SelectedIndex propriété avec la TopIndex propriété pour déplacer l’élément sélectionné en haut de la liste des éléments dans la zone d’affichage du ListBox. L’exemple montre également comment supprimer des éléments à l’aide de la RemoveAt méthode de la System.Windows.Forms.ListBox.ObjectCollection classe et comment effacer toute la sélection d’éléments à l’aide de la ClearSelected méthode. Le code déplace d’abord l’élément actuellement sélectionné en ListBox haut de la liste. Le code supprime ensuite tous les éléments avant l’élément actuellement sélectionné et efface toutes les sélections dans le ListBox. Cet exemple nécessite qu’un ListBox élément contenant soit ajouté à un formulaire et qu’un élément soit actuellement sélectionné dans le ListBoxfichier .
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
Remarques
L’appel de cette méthode équivaut à définir la SelectedIndex propriété sur négative (-1). Vous pouvez utiliser cette méthode pour désélectionner rapidement tous les éléments de la liste.