ListViewGroupCollection.Clear Methode
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Entfernt alle Gruppen aus der Auflistung.
public:
virtual void Clear();
public void Clear();
abstract member Clear : unit -> unit
override this.Clear : unit -> unit
Public Sub Clear ()
Implementiert
Beispiele
Im folgenden Beispiel wird veranschaulicht, wie die Clear Methode in einer Anwendung verwendet werden kann, die Elemente nach Unterelementwert in der Detailansicht organisiert ListView . Diese Form der Gruppierung ähnelt der Gruppierung, die im Windows-Explorer verwendet wird. Im Beispiel werden die Gruppen dynamisch erstellt. Für jede Unterelementspalte wird eine Gruppe für jeden eindeutigen Unterelementwert erstellt. Für die übergeordnete Elementspalte wird für jeden eindeutigen Anfangsbuchstaben eine Gruppe erstellt. Die für jede Spalte erstellten Gruppen werden zusammen mit dem Unterelementtext oder dem Anfangsbuchstaben in einer Hashtabelle gespeichert. Wenn auf eine Spaltenüberschrift geklickt wird, wird sie ListViewGroupCollection gelöscht. Die Hashtabelle, die der geklickten Spalte entspricht, wird dann abgerufen, und jedem Element wird die entsprechende Gruppe zugewiesen. Schließlich wird dem Hashtabelle ein sortiertes Array der Gruppen in der ListViewGroupCollectionHashtabelle hinzugefügt.
Das vollständige Beispiel finden Sie im ListViewGroupCollection Übersichtsreferenzthema.
// 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
Hinweise
Verwenden Sie diese Methode, um alle Gruppen aus der Auflistung zu entfernen. Beachten Sie, dass das Entfernen von Gruppen aus der ListView.Groups Auflistung keine Elemente aus dem ListView Steuerelement entfernt.
Diese Methode ist hilfreich, um das Gruppierungsfeature zu deaktivieren. Wenn keine Gruppen in einem ListView Steuerelement vorhanden sind, werden die Elemente normal angezeigt. Wenn Sie einzelne Gruppen aus der Auflistung entfernen möchten, verwenden Sie die Remove oder RemoveAt die Methode.
Diese Methode ist auch hilfreich, wenn Sie mehrere Möglichkeiten zum Gruppieren der Elemente bereitstellen möchten. Um die Gruppierung zu ändern, verwenden Sie zuerst die Clear Methode, um alle Gruppen aus der Auflistung zu entfernen, und verwenden Sie dann die AddRange Methode, um ein anderes Array von Gruppen hinzuzufügen.