Sdílet prostřednictvím


ListViewGroupCollection.Clear Metoda

Definice

Odebere všechny skupiny z kolekce.

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

Implementuje

Příklady

Následující příklad ukazuje, jak lze metodu Clear použít v aplikaci, která uspořádá ListView položky podle hodnoty subitem v zobrazení podrobností. Tato forma seskupení se podobá seskupení použitému v Windows Exploreru. V tomto příkladu se skupiny vytvářejí dynamicky. Pro každý sloupec podsítě se vytvoří jedna skupina pro každou jedinečnou hodnotu podsítě. Pro sloupec nadřazené položky se vytvoří jedna skupina pro každé jedinečné počáteční písmeno. Skupiny vytvořené pro každý sloupec jsou uloženy v tabulce hash spolu s textem subitem nebo počátečním písmenem. Po kliknutí na ListViewGroupCollection záhlaví sloupce se vymaže. Tabulka hash odpovídající klikaným sloupcům se pak načte a každá položka je přiřazena příslušné skupině. Nakonec se do tabulky hash přidá ListViewGroupCollectionseřazené pole skupin v tabulce hash .

Úplný příklad najdete v referenčním tématu s přehledem 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

Poznámky

Tuto metodu použijte k odebrání všech skupin z kolekce. Všimněte si, že odebrání skupin z ListView.Groups kolekce neodebere položky z ListView ovládacího prvku.

Tato metoda je užitečná k zakázání funkce seskupení. Pokud ovládací prvek neobsahuje ListView žádné skupiny, zobrazí se položky normálně. Pokud chcete odebrat jednotlivé skupiny z kolekce, použijte metodu nebo RemoveAt metoduRemove.

Tato metoda je užitečná také v případě, že chcete poskytnout několik způsobů seskupení položek. Pokud chcete změnit seskupení, nejprve pomocí Clear metody odeberte všechny skupiny z kolekce a pak pomocí AddRange metody přidejte jinou matici skupin.

Platí pro