ListViewGroupCollection.Clear メソッド
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)


コレクションからすべてのグループを削除するには、このメソッドを使用します。ListView.Groups コレクションからグループを削除しても、ListView コントロールの項目は削除されないことに注意してください。
このメソッドは、グループ化機能を無効にする場合に便利です。ListView コントロールにグループがない場合、項目は通常どおりに表示されます。コレクションから個々のグループを削除するには、Remove メソッドまたは RemoveAt メソッドを使用します。
項目をグループ化する方法を複数用意する場合にも、このメソッドは便利です。グループを変更するには、まず Clear メソッドを使用してコレクションのすべてのグループを削除してから、AddRange メソッドを使用して別のグループ配列を追加します。

詳細ビューのサブ項目の値を使って ListView 項目を整理するアプリケーションで、Clear メソッドを使用する方法を次の例に示します。この形式のグループ化は、Windows エクスプローラで使用されているグループ化と似ています。この例では、グループが動的に作成されます。各サブ項目列では、一意のサブ項目値ごとに 1 つのグループが作成されます。親項目列では、一意の先頭の文字ごとに 1 つのグループが作成されます。各列について作成されたグループは、サブ項目のテキストまたは先頭の文字と共にハッシュ テーブルに格納されます。列ヘッダーがクリックされると、ListViewGroupCollection がクリアされます。次に、クリックされた列に対応するハッシュ テーブルが取得され、各項目が適切なグループに割り当てられます。最後に、並べ替えられたハッシュ テーブル内のグループの配列が ListViewGroupCollection に追加されます。
コード全体については、ListViewGroupCollection の概要のリファレンス トピックを参照してください。
' 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 'SetGroups
// 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: 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.get_Groups().Clear(); // Retrieve the hash table corresponding to the column. Hashtable groups = (Hashtable)groupTables.get_Item(column); // Iterate through the items in myListView, assigning each // one to the appropriate group. for (int iCtr = 0; iCtr < myListView.get_Items().get_Count(); iCtr++) { ListViewItem item = myListView.get_Items().get_Item(iCtr); // Retrieve the subitem text corresponding to the column. String subItemText = item.get_SubItems().get_Item(column).get_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.set_Group((ListViewGroup)(groups.get_Item(subItemText))); } // Copy the groups for the column to an array. ListViewGroup groupsArray[] = new ListViewGroup[groups.get_Count()]; groups.get_Values().CopyTo(groupsArray, 0); // Sort the groups and add them to myListView. Array.Sort(groupsArray, new ListViewGroupSorter( myListView.get_Sorting())); myListView.get_Groups().AddRange(groupsArray); } //SetGroups

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からListViewGroupCollection.Clear メソッドを検索する場合は、下記のリンクをクリックしてください。

- ListViewGroupCollection.Clear メソッドのページへのリンク