ListViewGroupCollection.Clear Metod

Definition

Tar bort alla grupper från samlingen.

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

Implementeringar

Exempel

I följande exempel visas hur Clear metoden kan användas i ett program som organiserar ListView objekt efter underobjektvärde i informationsvyn. Den här typen av gruppering liknar den gruppering som används i Windows Explorer. I exemplet skapas grupperna dynamiskt. För varje underwebbplatskolumn skapas en grupp för varje unikt underobjektvärde. För den överordnade objektkolumnen skapas en grupp för varje unik inledande bokstav. De grupper som skapas för varje kolumn lagras i en hash-tabell tillsammans med underwebbplatstexten eller den inledande bokstaven. När du klickar på en kolumnrubrik rensas den ListViewGroupCollection . Den hash-tabell som motsvarar den klickade kolumnen hämtas sedan och varje objekt tilldelas till lämplig grupp. Slutligen läggs en sorterad matris med grupperna i hash-tabellen till i ListViewGroupCollection.

Det fullständiga exemplet finns i översiktsreferensavsnittet 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

Kommentarer

Använd den här metoden för att ta bort alla grupper från samlingen. Observera att borttagning av ListView.Groups grupper från samlingen inte tar bort objekt från ListView kontrollen.

Den här metoden är användbar för att inaktivera grupperingsfunktionen. När det inte finns några grupper i en ListView kontroll visas objekten normalt. Om du vill ta bort enskilda grupper från samlingen använder du Remove metoden eller RemoveAt .

Den här metoden är också användbar när du vill ange flera sätt att gruppera objekten. Om du vill ändra gruppering använder Clear du först metoden för att ta bort alla grupper från samlingen och använder AddRange sedan metoden för att lägga till en annan matris med grupper.

Gäller för