ListViewGroupCollection.AddRange Metoda

Definicja

Dodaje wiele grup do kolekcji.

Przeciążenia

AddRange(ListViewGroup[])

Dodaje tablicę grup do kolekcji.

AddRange(ListViewGroupCollection)

Dodaje grupy w istniejącej ListViewGroupCollection kolekcji.

AddRange(ListViewGroup[])

Dodaje tablicę grup do kolekcji.

public:
 void AddRange(cli::array <System::Windows::Forms::ListViewGroup ^> ^ groups);
public:
 void AddRange(... cli::array <System::Windows::Forms::ListViewGroup ^> ^ groups);
public void AddRange (System.Windows.Forms.ListViewGroup[] groups);
public void AddRange (params System.Windows.Forms.ListViewGroup[] groups);
member this.AddRange : System.Windows.Forms.ListViewGroup[] -> unit
Public Sub AddRange (groups As ListViewGroup())
Public Sub AddRange (ParamArray groups As ListViewGroup())

Parametry

groups
ListViewGroup[]

Tablica typu ListViewGroup określająca grupy do dodania do kolekcji.

Wyjątki

groups zawiera co najmniej jedną grupę z co najmniej jedną ListViewItem , która należy do kontrolki innej ListView niż ta, która jest właścicielem tego ListViewGroupCollection.

Przypisana ListView do tej kolekcji jest w trybie wirtualnym.

Przykłady

W poniższym przykładzie pokazano, jak AddRange można użyć metody w aplikacji, która organizuje ListView elementy według wartości subitem w widoku szczegółów. Ta forma grupowania jest podobna do grupowania używanego w Eksploratorze Windows. W tym przykładzie grupy są tworzone dynamicznie. Dla każdej kolumny subitem jedna grupa jest tworzona dla każdej unikatowej wartości subitem. Dla kolumny elementu nadrzędnego jedna grupa jest tworzona dla każdej unikatowej litery początkowej. Grupy utworzone dla każdej kolumny są przechowywane w tabeli skrótów wraz z tekstem subitem lub początkową literą. Po kliknięciu ListViewGroupCollection nagłówka kolumny zostanie wyczyszczone. Następnie zostanie pobrana tabela skrótów odpowiadająca klikniętej kolumnie, a każdy element zostanie przypisany do odpowiedniej grupy. Na koniec do tabeli skrótów jest dodawana ListViewGroupCollectionposortowana tablica grup .

Pełny przykład można znaleźć w temacie referencyjnym ListViewGroupCollection przeglądu.

   // 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

Uwagi

Użyj tej wersji AddRange metody, aby dodać tablicę grup do kolekcji grup. Ta metoda jest przydatna podczas tworzenia wielu ListViewGroup obiektów i dodawania ich do kolekcji za pomocą pojedynczego wywołania metody. Aby dodać poszczególne grupy do kolekcji, użyj Add metody .

Ta metoda jest również przydatna, gdy chcesz udostępnić wiele sposobów grupowania elementów w kontrolce ListView . W tym celu utwórz wiele tablic grupowych. Aby zmienić grupowanie, najpierw użyj Clear metody , aby usunąć wszystkie grupy z kolekcji, a następnie użyj AddRange metody , aby dodać inną tablicę grup.

Add W przeciwieństwie do metody metoda nie ma wartości zwracanej, która może służyć do określenia, AddRange czy dodawana grupa znajduje się już w kolekcji. Jeśli potrzebujesz tych informacji, użyj Contains metody przed użyciem AddRange metody .

Zobacz też

Dotyczy

AddRange(ListViewGroupCollection)

Dodaje grupy w istniejącej ListViewGroupCollection kolekcji.

public:
 void AddRange(System::Windows::Forms::ListViewGroupCollection ^ groups);
public void AddRange (System.Windows.Forms.ListViewGroupCollection groups);
member this.AddRange : System.Windows.Forms.ListViewGroupCollection -> unit
Public Sub AddRange (groups As ListViewGroupCollection)

Parametry

groups
ListViewGroupCollection

Element ListViewGroupCollection zawierający grupy do dodania do kolekcji.

Wyjątki

groups zawiera co najmniej jedną grupę z co najmniej jedną ListViewItem , która należy do kontrolki innej ListView niż ta, która jest właścicielem tego ListViewGroupCollection.

Przypisana ListView do tej kolekcji jest w trybie wirtualnym.

Uwagi

Użyj tej wersji AddRange metody, aby dodać elementy ListViewGroupCollection elementu, które są pobierane za pośrednictwem ListView.Groups właściwości innej ListView kontrolki.

Add W przeciwieństwie do metody metoda nie ma wartości zwracanej, która może służyć do określenia, AddRange czy dodawana grupa znajduje się już w kolekcji. Jeśli potrzebujesz tych informacji, użyj Contains metody przed użyciem AddRange metody .

Dotyczy