ListViewGroup 建構函式

定義

初始化 ListViewGroup 類別的新執行個體。

多載

ListViewGroup()

使用預設頁首文字 "ListViewGroup" 和預設左行首對齊方式,初始化 ListViewGroup 類別的新執行個體。

ListViewGroup(String)

使用指定的值初始化 ListViewGroup 屬性,並使用預設左行首對齊方式,初始化 Header 類別的新執行個體。

ListViewGroup(String, String)

使用指定的值初始化 ListViewGroupName 屬性,初始化 Header 類別的新執行個體。

ListViewGroup(String, HorizontalAlignment)

使用指定的頁首文字和指定的行首對齊方式,初始化 ListViewGroup 類別的新執行個體。

ListViewGroup()

使用預設頁首文字 "ListViewGroup" 和預設左行首對齊方式,初始化 ListViewGroup 類別的新執行個體。

public:
 ListViewGroup();
public ListViewGroup ();
Public Sub New ()

適用於

ListViewGroup(String)

使用指定的值初始化 ListViewGroup 屬性,並使用預設左行首對齊方式,初始化 Header 類別的新執行個體。

public:
 ListViewGroup(System::String ^ header);
public ListViewGroup (string header);
public ListViewGroup (string? header);
new System.Windows.Forms.ListViewGroup : string -> System.Windows.Forms.ListViewGroup
Public Sub New (header As String)

參數

header
String

要顯示的群組頁首文字。

適用於

ListViewGroup(String, String)

使用指定的值初始化 ListViewGroupName 屬性,初始化 Header 類別的新執行個體。

public:
 ListViewGroup(System::String ^ key, System::String ^ headerText);
public ListViewGroup (string key, string headerText);
public ListViewGroup (string? key, string? headerText);
new System.Windows.Forms.ListViewGroup : string * string -> System.Windows.Forms.ListViewGroup
Public Sub New (key As String, headerText As String)

參數

key
String

Name 屬性的初始值。

headerText
String

Header 屬性的初始值。

適用於

ListViewGroup(String, HorizontalAlignment)

使用指定的頁首文字和指定的行首對齊方式,初始化 ListViewGroup 類別的新執行個體。

public:
 ListViewGroup(System::String ^ header, System::Windows::Forms::HorizontalAlignment headerAlignment);
public ListViewGroup (string header, System.Windows.Forms.HorizontalAlignment headerAlignment);
public ListViewGroup (string? header, System.Windows.Forms.HorizontalAlignment headerAlignment);
new System.Windows.Forms.ListViewGroup : string * System.Windows.Forms.HorizontalAlignment -> System.Windows.Forms.ListViewGroup
Public Sub New (header As String, headerAlignment As HorizontalAlignment)

參數

header
String

要顯示的群組頁首文字。

headerAlignment
HorizontalAlignment

其中一個 HorizontalAlignment 值,指定頁首文字的對齊方式。

範例

下列程式碼範例示範如何在應用程式中使用建構函式,以 ListViewGroup 詳細資料檢視中的子專案值來組織 ListView 專案。 這種形式的群組類似于 Windows Explorer 中使用的群組。 在此範例中,會動態建立群組。 針對每個子專案資料行,會為每個唯一子專案值建立一個群組。 針對父專案資料行,會為每個唯一的初始字母建立一個群組。 針對每個資料行建立的群組會儲存在雜湊表中,以及子專案文字或初始字母。 按一下資料行標頭時,此文字值會用來比對適當資料行的專案與群組。

如需完整的範例,請參閱 ListViewGroup 概觀參考主題。

   // Creates a Hashtable object with one entry for each unique
   // subitem value (or initial letter for the parent item)
   // in the specified column.
private:
   Hashtable^ CreateGroupsTable(int column)
   {
      // Create a Hashtable object.
      Hashtable^ groups = gcnew Hashtable();

      // Iterate through the items in myListView.
      IEnumerator^ myEnum1 = myListView->Items->GetEnumerator();
      while (myEnum1->MoveNext())
      {
         ListViewItem^ item = safe_cast<ListViewItem^>(myEnum1->Current);
         // Retrieve the text value for the column.
         String^ subItemText = item->SubItems[column]->Text;

         // Use the initial letter instead if it is the first column.
         if (column == 0) 
         {
            subItemText = subItemText->Substring(0, 1);
         }

         // If the groups table does not already contain a group
         // for the subItemText value, add a new group using the 
         // subItemText value for the group header and Hashtable key.
         if (!groups->Contains(subItemText))
         {
            groups->Add( subItemText, gcnew ListViewGroup(subItemText, 
               HorizontalAlignment::Left) );
         }
      }

      // Return the Hashtable object.
      return groups;
   }
// Creates a Hashtable object with one entry for each unique
// subitem value (or initial letter for the parent item)
// in the specified column.
private Hashtable CreateGroupsTable(int column)
{
    // Create a Hashtable object.
    Hashtable groups = new Hashtable();

    // Iterate through the items in myListView.
    foreach (ListViewItem item in myListView.Items)
    {
        // Retrieve the text value for the column.
        string subItemText = item.SubItems[column].Text;

        // Use the initial letter instead if it is the first column.
        if (column == 0) 
        {
            subItemText = subItemText.Substring(0, 1);
        }

        // If the groups table does not already contain a group
        // for the subItemText value, add a new group using the 
        // subItemText value for the group header and Hashtable key.
        if (!groups.Contains(subItemText))
        {
            groups.Add( subItemText, new ListViewGroup(subItemText, 
                HorizontalAlignment.Left) );
        }
    }

    // Return the Hashtable object.
    return groups;
}
' Creates a Hashtable object with one entry for each unique
' subitem value (or initial letter for the parent item)
' in the specified column.
Private Function CreateGroupsTable(column As Integer) As Hashtable
    ' Create a Hashtable object.
    Dim groups As New Hashtable()
    
    ' Iterate through the items in myListView.
    Dim item As ListViewItem
    For Each item In myListView.Items
        ' Retrieve the text value for the column.
        Dim subItemText As String = item.SubItems(column).Text
        
        ' Use the initial letter instead if it is the first column.
        If column = 0 Then
            subItemText = subItemText.Substring(0, 1)
        End If 

        ' If the groups table does not already contain a group
        ' for the subItemText value, add a new group using the 
        ' subItemText value for the group header and Hashtable key.
        If Not groups.Contains(subItemText) Then
            groups.Add( subItemText, New ListViewGroup(subItemText, _
                HorizontalAlignment.Left) )
        End If
    Next item
    
    ' Return the Hashtable object.
    Return groups
End Function 'CreateGroupsTable

適用於