ListViewGroup Constructors

Definition

初始化 ListViewGroup 类的新实例。Initializes a new instance of the ListViewGroup class.

Overloads

ListViewGroup()

用标题文本的默认值“ListViewGroup”和标题默认的左对齐方式初始化 ListViewGroup 类的新实例。Initializes a new instance of the ListViewGroup class using the default header text of "ListViewGroup" and the default left header alignment.

ListViewGroup(String)

通过使用指定值初始化 ListViewGroup 属性以及使用默认的左标题对齐方式初始化 Header 类的新实例。Initializes a new instance of the ListViewGroup class using the specified value to initialize the Header property and using the default left header alignment.

ListViewGroup(String, String)

通过使用指定值初始化 ListViewGroupName 属性初始化 Header 类的新实例。Initializes a new instance of the ListViewGroup class using the specified values to initialize the Name and Header properties.

ListViewGroup(String, HorizontalAlignment)

用指定的标题文本和指定的标题对齐方式初始化 ListViewGroup 类的新实例。Initializes a new instance of the ListViewGroup class using the specified header text and the specified header alignment.

ListViewGroup()

用标题文本的默认值“ListViewGroup”和标题默认的左对齐方式初始化 ListViewGroup 类的新实例。Initializes a new instance of the ListViewGroup class using the default header text of "ListViewGroup" and the default left header alignment.

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

ListViewGroup(String)

通过使用指定值初始化 ListViewGroup 属性以及使用默认的左标题对齐方式初始化 Header 类的新实例。Initializes a new instance of the ListViewGroup class using the specified value to initialize the Header property and using the default left header alignment.

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

Parameters

header
String

要为组标题显示的文本。The text to display for the group header.

ListViewGroup(String, String)

通过使用指定值初始化 ListViewGroupName 属性初始化 Header 类的新实例。Initializes a new instance of the ListViewGroup class using the specified values to initialize the Name and Header properties.

public:
 ListViewGroup(System::String ^ key, System::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)

Parameters

key
String

Name 属性的初始值。The initial value of the Name property.

headerText
String

Header 属性的初始值。The initial value of the Header property.

ListViewGroup(String, HorizontalAlignment)

用指定的标题文本和指定的标题对齐方式初始化 ListViewGroup 类的新实例。Initializes a new instance of the ListViewGroup class using the specified header text and the specified header alignment.

public:
 ListViewGroup(System::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)

Parameters

header
String

要为组标题显示的文本。The text to display for the group header.

headerAlignment
HorizontalAlignment

HorizontalAlignment 值之一,它指定标题文本的对齐方式。One of the HorizontalAlignment values that specifies the alignment of the header text.

Examples

下面的代码示例演示如何在应用程序中使用 ListViewGroup 构造函数,该应用程序按详细信息视图中的子项值组织 ListView 项。The following code example demonstrates how the ListViewGroup constructor can be used in an application that organizes ListView items by subitem value in the details view. 这种形式的分组类似于 Windows 资源管理器中使用的分组。This form of grouping is similar to the grouping used in Windows Explorer. 在此示例中,将动态创建组。In the example, the groups are created dynamically. 对于每个子项列,将为每个唯一子项值创建一个组。For each subitem column, one group is created for each unique subitem value. 对于 "父项" 列,将为每个唯一的初始字母创建一个组。For the parent item column, one group is created for each unique initial letter. 为每个列创建的组与子项文本或首字母一起存储在哈希表中。The groups created for each column are stored in a hash table along with the subitem text or initial letter. 单击列标题时,此文本值用于将相应列的项与组进行匹配。When a column header is clicked, this text value is used to match items to groups for the appropriate column.

有关完整示例,请参阅 ListViewGroup 概述参考主题。For the complete example, see the ListViewGroup overview reference topic.

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

Applies to