ListViewGroupCollection.Clear Méthode

Définition

Supprime tous les groupes de la collection.

public:
 virtual void Clear();
public void Clear ();
abstract member Clear : unit -> unit
override this.Clear : unit -> unit
Public Sub Clear ()

Implémente

Exemples

L’exemple suivant montre comment la Clear méthode peut être utilisée dans une application qui organise les ListView éléments par valeur sous-élément dans la vue détails. Cette forme de regroupement est similaire au regroupement utilisé dans Windows Explorateur. Dans l’exemple, les groupes sont créés dynamiquement. Pour chaque colonne sous-élément, un groupe est créé pour chaque valeur sous-élément unique. Pour la colonne d’élément parent, un groupe est créé pour chaque lettre initiale unique. Les groupes créés pour chaque colonne sont stockés dans une table de hachage avec le texte sous-élément ou la lettre initiale. Lorsqu’un en-tête de colonne est cliqué, l’en-tête ListViewGroupCollection est effacé. La table de hachage correspondant à la colonne cliquée est ensuite récupérée et chaque élément est affecté au groupe approprié. Enfin, un tableau trié des groupes dans la table de hachage est ajouté au ListViewGroupCollection.

Pour obtenir l’exemple complet, consultez la rubrique de référence de vue d’ensemble ListViewGroupCollection .

   // Sets myListView to the groups created for the specified column.
private:
   void SetGroups(int column)
   {
      // Remove the current groups.
      myListView->Groups->Clear();

      // Retrieve the hash table corresponding to the column.
      Hashtable^ groups = dynamic_cast<Hashtable^>(groupTables[column]);

      // Copy the groups for the column to an array.
      array<ListViewGroup^>^ groupsArray = gcnew array<ListViewGroup^>(groups->Count);
      groups->Values->CopyTo(groupsArray, 0);

      // Sort the groups and add them to myListView.
      Array::Sort(groupsArray, gcnew ListViewGroupSorter(myListView->Sorting));
      myListView->Groups->AddRange(groupsArray);

      // Iterate through the items in myListView, assigning each 
      // one to the appropriate group.
      IEnumerator^ myEnum = myListView->Items->GetEnumerator();
      while (myEnum->MoveNext())
      {
         ListViewItem^ item = safe_cast<ListViewItem^>(myEnum->Current);
         // Retrieve the subitem text corresponding to the column.
         String^ subItemText = item->SubItems[column]->Text;

         // For the Title column, use only the first letter.
         if (column == 0) 
         {
            subItemText = subItemText->Substring(0, 1);
         }

         // Assign the item to the matching group.
         item->Group = dynamic_cast<ListViewGroup^>(groups[subItemText]);
      }
   }
// Sets myListView to the groups created for the specified column.
private void SetGroups(int column)
{
    // Remove the current groups.
    myListView.Groups.Clear();

    // Retrieve the hash table corresponding to the column.
    Hashtable groups = (Hashtable)groupTables[column];

    // Copy the groups for the column to an array.
    ListViewGroup[] groupsArray = new ListViewGroup[groups.Count];
    groups.Values.CopyTo(groupsArray, 0);

    // Sort the groups and add them to myListView.
    Array.Sort(groupsArray, new ListViewGroupSorter(myListView.Sorting));
    myListView.Groups.AddRange(groupsArray);

    // Iterate through the items in myListView, assigning each 
    // one to the appropriate group.
    foreach (ListViewItem item in myListView.Items)
    {
        // Retrieve the subitem text corresponding to the column.
        string subItemText = item.SubItems[column].Text;

        // For the Title column, use only the first letter.
        if (column == 0) 
        {
            subItemText = subItemText.Substring(0, 1);
        }

        // Assign the item to the matching group.
        item.Group = (ListViewGroup)groups[subItemText];
    }
}
' Sets myListView to the groups created for the specified column.
Private Sub SetGroups(column As Integer)
    ' Remove the current groups.
    myListView.Groups.Clear()
    
    ' Retrieve the hash table corresponding to the column.
    Dim groups As Hashtable = CType(groupTables(column), Hashtable)
    
    ' Copy the groups for the column to an array.
    Dim groupsArray(groups.Count - 1) As ListViewGroup
    groups.Values.CopyTo(groupsArray, 0)
    
    ' Sort the groups and add them to myListView.
    Array.Sort(groupsArray, New ListViewGroupSorter(myListView.Sorting))
    myListView.Groups.AddRange(groupsArray)
    
    ' Iterate through the items in myListView, assigning each 
    ' one to the appropriate group.
    Dim item As ListViewItem
    For Each item In myListView.Items
        ' Retrieve the subitem text corresponding to the column.
        Dim subItemText As String = item.SubItems(column).Text
        
        ' For the Title column, use only the first letter.
        If column = 0 Then
            subItemText = subItemText.Substring(0, 1)
        End If 

        ' Assign the item to the matching group.
        item.Group = CType(groups(subItemText), ListViewGroup)
    Next item
End Sub

Remarques

Utilisez cette méthode pour supprimer tous les groupes de la collection. Notez que la suppression de groupes de la ListView.Groups collection ne supprime pas les éléments du ListView contrôle.

Cette méthode est utile pour désactiver la fonctionnalité de regroupement. Lorsqu’aucun groupe n’est présent dans un ListView contrôle, les éléments apparaissent normalement. Pour supprimer des groupes individuels de la collection, utilisez la ou RemoveAt la Remove méthode.

Cette méthode est également utile lorsque vous souhaitez fournir plusieurs façons de regrouper les éléments. Pour modifier le regroupement, utilisez d’abord la Clear méthode pour supprimer tous les groupes de la collection, puis utilisez la AddRange méthode pour ajouter un autre tableau de groupes.

S’applique à