ListViewGroupCollection.Clear Metoda
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
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á seskupování použitému v Průzkumníku Windows. 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. Když kliknete na záhlaví sloupce, vymaže se ListViewGroupCollection . Potom se načte tabulka hash odpovídající kliknutí na sloupec a každá položka se přiřadí příslušné skupině. Nakonec se do ListViewGroupCollectiontabulky hash přidá seřazené pole skupin v tabulce hash .
Úplný příklad najdete v referenčním tématu přehledu 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í ListView prvek neobsahuje žádné skupiny, zobrazí se položky normálně. Pokud chcete z kolekce odebrat jednotlivé skupiny, použijte metodu nebo Remove metoduRemoveAt.
Tato metoda je užitečná také v případě, že chcete poskytnout více 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 jiné pole skupin.