Condividi tramite


ListViewGroupCollection.AddRange Metodo

Definizione

Aggiunge più gruppi alla raccolta.

Overload

Nome Descrizione
AddRange(ListViewGroup[])

Aggiunge una matrice di gruppi all'insieme.

AddRange(ListViewGroupCollection)

Aggiunge i gruppi in un oggetto esistente ListViewGroupCollection all'insieme.

AddRange(ListViewGroup[])

Origine:
ListViewGroupCollection.cs
Origine:
ListViewGroupCollection.cs
Origine:
ListViewGroupCollection.cs
Origine:
ListViewGroupCollection.cs
Origine:
ListViewGroupCollection.cs

Aggiunge una matrice di gruppi all'insieme.

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())

Parametri

groups
ListViewGroup[]

Matrice di tipo ListViewGroup che specifica i gruppi da aggiungere alla raccolta.

Eccezioni

groups contiene almeno un gruppo con almeno uno ListViewItem che appartiene a un ListView controllo diverso da quello proprietario di questo ListViewGroupCollectionoggetto .

L'oggetto ListView a cui è assegnata questa raccolta è in modalità virtuale.

Esempio

Nell'esempio seguente viene illustrato come usare il AddRange metodo in un'applicazione che organizza ListView gli elementi in base al valore dell'elemento secondario nella visualizzazione dei dettagli. Questa forma di raggruppamento è simile al raggruppamento usato in Esplora risorse. Nell'esempio i gruppi vengono creati in modo dinamico. Per ogni colonna dell'elemento secondario, viene creato un gruppo per ogni valore dell'elemento secondario univoco. Per la colonna dell'elemento padre, viene creato un gruppo per ogni lettera iniziale univoca. I gruppi creati per ogni colonna vengono archiviati in una tabella hash insieme al testo dell'elemento secondario o alla lettera iniziale. Quando si fa clic su un'intestazione di colonna, l'oggetto ListViewGroupCollection viene cancellato. La tabella hash corrispondente alla colonna selezionata viene quindi recuperata e ogni elemento viene assegnato al gruppo appropriato. Infine, viene aggiunta una matrice ordinata dei gruppi nella tabella hash all'oggetto ListViewGroupCollection.

Per l'esempio completo, vedere l'argomento di riferimento di ListViewGroupCollection panoramica.

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

Commenti

Utilizzare questa versione del AddRange metodo per aggiungere una matrice di gruppi alla raccolta di gruppi. Questo metodo è utile quando si creano più ListViewGroup oggetti e si desidera aggiungerli alla raccolta con una singola chiamata al metodo. Per aggiungere singoli gruppi alla raccolta, utilizzare il Add metodo .

Questo metodo è utile anche quando si desidera fornire più modi per raggruppare gli elementi in un ListView controllo. A tale scopo, creare più matrici di gruppi. Per modificare il raggruppamento, usare prima di tutto il Clear metodo per rimuovere tutti i gruppi dalla raccolta, quindi usare il AddRange metodo per aggiungere una matrice diversa di gruppi.

A differenza del Add metodo , il AddRange metodo non dispone di un valore restituito che può essere utilizzato per determinare se un gruppo da aggiungere è già presente nella raccolta. Se sono necessarie queste informazioni, usare il Contains metodo prima di usare il AddRange metodo .

Vedi anche

Si applica a

AddRange(ListViewGroupCollection)

Origine:
ListViewGroupCollection.cs
Origine:
ListViewGroupCollection.cs
Origine:
ListViewGroupCollection.cs
Origine:
ListViewGroupCollection.cs
Origine:
ListViewGroupCollection.cs

Aggiunge i gruppi in un oggetto esistente ListViewGroupCollection all'insieme.

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)

Parametri

groups
ListViewGroupCollection

Oggetto ListViewGroupCollection contenente i gruppi da aggiungere alla raccolta.

Eccezioni

groups contiene almeno un gruppo con almeno uno ListViewItem che appartiene a un ListView controllo diverso da quello proprietario di questo ListViewGroupCollectionoggetto .

L'oggetto ListView a cui è assegnata questa raccolta è in modalità virtuale.

Commenti

Utilizzare questa versione del AddRange metodo per aggiungere gli elementi di un oggetto ListViewGroupCollection recuperato tramite la ListView.Groups proprietà di un controllo diverso ListView .

A differenza del Add metodo , il AddRange metodo non dispone di un valore restituito che può essere utilizzato per determinare se un gruppo da aggiungere è già presente nella raccolta. Se sono necessarie queste informazioni, usare il Contains metodo prima di usare il AddRange metodo .

Si applica a