ListView.ColumnHeaderCollection 类

表示 ListView 控件中列标头的集合。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Class ColumnHeaderCollection
    Implements IList, ICollection, IEnumerable
用法
Dim instance As ColumnHeaderCollection
public class ColumnHeaderCollection : IList, ICollection, IEnumerable
public ref class ColumnHeaderCollection : IList, ICollection, IEnumerable
public class ColumnHeaderCollection implements IList, ICollection, 
    IEnumerable
public class ColumnHeaderCollection implements IList, ICollection, 
    IEnumerable

备注

ListView.ColumnHeaderCollection 类存储 View 属性设置为 Details 时显示在 ListView 中的列标头。ListView.ColumnHeaderCollection 存储 ColumnHeader 对象,这些对象定义要为列显示的文本,以及显示列时列标头如何在 ListView 控件中显示。当 ListView 显示列时,项及其子项在它们自己的列中显示。要指定子项数据在哪些列中显示,请参见 ListViewItem.ListViewSubItemCollection 类。

有多种方法可以给集合添加列标头。Add 方法给集合添加单个列标头。要给集合中添加多个列标头,请创建 ColumnHeader 对象数组,并将其传递至 AddRange 方法。如果要在集合中的特定位置插入列标头,可使用 Insert 方法。要移除列标头,可使用 Remove 方法或 RemoveAt 方法(如果您知道列标头在集合中的位置)。Clear 方法允许您从集合中移除所有列标头,而不是使用 Remove 方法一次移除一个列标头。

除了用于添加和移除列标头的方法及属性,ListView.ColumnHeaderCollection 还提供了在集合中查找列标头的方法。Contains 方法使您能够确定某个列标头是否是集合的成员。知道列标头位于集合中后,可使用 IndexOf 方法来确定该列标头在集合中的位置。

提示

如果所有列的组合宽度超过 32,768 个像素,则会导致异常行为。

示例

下面的代码示例创建包含 ListView 控件的窗体,在单击 ListView 控件中的列时可对各项进行手动排序。此示例定义了一个名为 ListViewItemComparer 的类,该类可实现执行 ListViewItem 比较的 System.Collections.IComparer 接口。此示例创建了 ListViewItemComparer 的一个实例,并用它来设置 ListView 控件的 ListViewItemSorter 属性。ColumnClick 事件处理程序中的 Sort 方法调用使用 ListViewItemComparer 中定义的方法根据被单击的列来执行项的排序。

Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Collections


Namespace ListViewSortFormNamespace

    Public Class ListViewSortForm
        Inherits Form

        Private listView1 As ListView

        Public Sub New()
            ' Create ListView items to add to the control.
            Dim listViewItem1 As New ListViewItem(New String() {"Banana", "a", "b", "c"}, -1, Color.Empty, Color.Yellow, Nothing)
            Dim listViewItem2 As New ListViewItem(New String() {"Cherry", "v", "g", "t"}, -1, Color.Empty, Color.Red, New Font("Microsoft Sans Serif", 8.25F, FontStyle.Regular, GraphicsUnit.Point, CType(0, System.Byte)))
            Dim listViewItem3 As New ListViewItem(New String() {"Apple", "h", "j", "n"}, -1, Color.Empty, Color.Lime, Nothing)
            Dim listViewItem4 As New ListViewItem(New String() {"Pear", "y", "u", "i"}, -1, Color.Empty, Color.FromArgb(CType(192, System.Byte), CType(128, System.Byte), CType(156, System.Byte)), Nothing)

            'Initialize the ListView control and add columns to it.
            Me.listView1 = New ListView

            ' Set the initial sorting type for the ListView.
            Me.listView1.Sorting = SortOrder.None
            ' Disable automatic sorting to enable manual sorting.
            Me.listView1.View = View.Details
            ' Add columns and set their text.
            Me.listView1.Columns.Add(New ColumnHeader)
            Me.listView1.Columns(0).Text = "Column 1"
            Me.listView1.Columns(0).Width = 100
            listView1.Columns.Add(New ColumnHeader)
            listView1.Columns(1).Text = "Column 2"
            listView1.Columns.Add(New ColumnHeader)
            listView1.Columns(2).Text = "Column 3"
            listView1.Columns.Add(New ColumnHeader)
            listView1.Columns(3).Text = "Column 4"
            ' Suspend control logic until form is done configuring form.
            Me.SuspendLayout()
            ' Add Items to the ListView control.
            Me.listView1.Items.AddRange(New ListViewItem() {listViewItem1, listViewItem2, listViewItem3, listViewItem4})
            ' Set the location and size of the ListView control.
            Me.listView1.Location = New Point(10, 10)
            Me.listView1.Name = "listView1"
            Me.listView1.Size = New Size(300, 100)
            Me.listView1.TabIndex = 0
            ' Enable editing of the items in the ListView.
            Me.listView1.LabelEdit = True
            ' Connect the ListView.ColumnClick event to the ColumnClick event handler.
            AddHandler Me.listView1.ColumnClick, AddressOf ColumnClick

            ' Initialize the form.
            Me.ClientSize = New Size(400, 400)
            Me.Controls.AddRange(New Control() {Me.listView1})
            Me.Name = "ListViewSortForm"
            Me.Text = "Sorted ListView Control"
            ' Resume layout of the form.
            Me.ResumeLayout(False)
        End Sub 'New


        ' ColumnClick event handler.
        Private Sub ColumnClick(ByVal o As Object, ByVal e As ColumnClickEventArgs)
            ' Set the ListViewItemSorter property to a new ListViewItemComparer 
            ' object. Setting this property immediately sorts the 
            ' ListView using the ListViewItemComparer object.
            Me.listView1.ListViewItemSorter = New ListViewItemComparer(e.Column)
        End Sub

    End Class

    ' Implements the manual sorting of items by columns.
    Class ListViewItemComparer
        Implements IComparer

        Private col As Integer

        Public Sub New()
            col = 0
        End Sub

        Public Sub New(ByVal column As Integer)
            col = column
        End Sub

        Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
           Implements IComparer.Compare
            Return [String].Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text)
        End Function
    End Class
End Namespace
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;

namespace ListViewSortFormNamespace
{
    
   
    public class ListViewSortForm : Form
    {
        private ListView listView1;
       
        public ListViewSortForm()
        {
            // Create ListView items to add to the control.
            ListViewItem listViewItem1 = new ListViewItem(new string[] {"Banana","a","b","c"}, -1, Color.Empty, Color.Yellow, null);
            ListViewItem listViewItem2 = new ListViewItem(new string[] {"Cherry","v","g","t"}, -1, Color.Empty, Color.Red, new Font("Microsoft Sans Serif", 8.25F, FontStyle.Regular, GraphicsUnit.Point, ((System.Byte)(0))));
            ListViewItem listViewItem3 = new ListViewItem(new string[] {"Apple","h","j","n"}, -1, Color.Empty, Color.Lime, null);
            ListViewItem listViewItem4 = new ListViewItem(new string[] {"Pear","y","u","i"}, -1, Color.Empty, Color.FromArgb(((System.Byte)(192)), ((System.Byte)(128)), ((System.Byte)(156))), null);
     
            //Initialize the ListView control and add columns to it.
            this.listView1 = new ListView();

            // Set the initial sorting type for the ListView.
            this.listView1.Sorting = SortOrder.None;
            // Disable automatic sorting to enable manual sorting.
            this.listView1.View = View.Details;
            // Add columns and set their text.
            this.listView1.Columns.Add(new ColumnHeader());
            this.listView1.Columns[0].Text = "Column 1";
            this.listView1.Columns[0].Width = 100;
            listView1.Columns.Add(new ColumnHeader());
            listView1.Columns[1].Text = "Column 2";
            listView1.Columns.Add(new ColumnHeader());
            listView1.Columns[2].Text = "Column 3";
            listView1.Columns.Add(new ColumnHeader());
            listView1.Columns[3].Text = "Column 4";
            // Suspend control logic until form is done configuring form.
            this.SuspendLayout();
            // Add Items to the ListView control.
            this.listView1.Items.AddRange(new ListViewItem[] {listViewItem1,
                listViewItem2,
                listViewItem3,
                listViewItem4});
            // Set the location and size of the ListView control.
            this.listView1.Location = new Point(10, 10);
            this.listView1.Name = "listView1";
            this.listView1.Size = new Size(300, 100);
            this.listView1.TabIndex = 0;
            // Enable editing of the items in the ListView.
            this.listView1.LabelEdit = true;
            // Connect the ListView.ColumnClick event to the ColumnClick event handler.
            this.listView1.ColumnClick += new ColumnClickEventHandler(ColumnClick);
            
            // Initialize the form.
            this.ClientSize = new Size(400, 400);
            this.Controls.AddRange(new Control[] {this.listView1});
            this.Name = "ListViewSortForm";
            this.Text = "Sorted ListView Control";
            // Resume layout of the form.
            this.ResumeLayout(false);
        }
        
    
        // ColumnClick event handler.
        private void ColumnClick(object o, ColumnClickEventArgs e)
        {
            // Set the ListViewItemSorter property to a new ListViewItemComparer 
            // object. Setting this property immediately sorts the 
            // ListView using the ListViewItemComparer object.
            this.listView1.ListViewItemSorter = new ListViewItemComparer(e.Column);
        }

        [System.STAThreadAttribute()]
        public static void Main()
        {
            Application.Run(new ListViewSortForm());
        }

    }

    // Implements the manual sorting of items by columns.
    class ListViewItemComparer : IComparer
    {
        private int col;
        public ListViewItemComparer()
        {
            col = 0;
        }
        public ListViewItemComparer(int column)
        {
            col = column;
        }
        public int Compare(object x, object y)
        {
            return String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
        }
    }

}
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Windows::Forms;
using namespace System::Drawing;
using namespace System::Collections;

// Implements the manual sorting of items by columns.
ref class ListViewItemComparer: public IComparer
{
private:
   int col;

public:
   ListViewItemComparer()
   {
      col = 0;
   }

   ListViewItemComparer( int column )
   {
      col = column;
   }

   virtual int Compare( Object^ x, Object^ y )
   {
      return String::Compare( (dynamic_cast<ListViewItem^>(x))->SubItems[ col ]->Text,
                              (dynamic_cast<ListViewItem^>(y))->SubItems[ col ]->Text );
   }
};

public ref class ListViewSortForm: public Form
{
private:
   ListView^ listView1;

public:
   ListViewSortForm()
   {
      // Create ListView items to add to the control.
      array<String^>^temp0 = {"Banana","a","b","c"};
      ListViewItem^ listViewItem1 = gcnew ListViewItem( temp0,-1,Color::Empty,Color::Yellow,nullptr );
      array<String^>^temp1 = {"Cherry","v","g","t"};
      ListViewItem^ listViewItem2 = gcnew ListViewItem( temp1,-1,Color::Empty,Color::Red,
                 gcnew System::Drawing::Font( "Microsoft Sans Serif",8.25F,FontStyle::Regular,GraphicsUnit::Point,0 ) );
      array<String^>^temp2 = {"Apple","h","j","n"};
      ListViewItem^ listViewItem3 = gcnew ListViewItem( temp2,-1,Color::Empty,Color::Lime,nullptr );
      array<String^>^temp3 = {"Pear","y","u","i"};
      ListViewItem^ listViewItem4 = gcnew ListViewItem( temp3,-1,Color::Empty,Color::FromArgb( 192, 128, 156 ),nullptr );

      //Initialize the ListView control and add columns to it.
      this->listView1 = gcnew ListView;

      // Set the initial sorting type for the ListView.
      this->listView1->Sorting = SortOrder::None;

      // Disable automatic sorting to enable manual sorting.
      this->listView1->View = View::Details;

      // Add columns and set their text.
      this->listView1->Columns->Add( gcnew ColumnHeader );
      this->listView1->Columns[ 0 ]->Text = "Column 1";
      this->listView1->Columns[ 0 ]->Width = 100;
      listView1->Columns->Add( gcnew ColumnHeader );
      listView1->Columns[ 1 ]->Text = "Column 2";
      listView1->Columns->Add( gcnew ColumnHeader );
      listView1->Columns[ 2 ]->Text = "Column 3";
      listView1->Columns->Add( gcnew ColumnHeader );
      listView1->Columns[ 3 ]->Text = "Column 4";

      // Suspend control logic until form is done configuring form.
      this->SuspendLayout();

      // Add Items to the ListView control.
      array<ListViewItem^>^temp4 = {listViewItem1,listViewItem2,listViewItem3,listViewItem4};
      this->listView1->Items->AddRange( temp4 );

      // Set the location and size of the ListView control.
      this->listView1->Location = Point(10,10);
      this->listView1->Name = "listView1";
      this->listView1->Size = System::Drawing::Size( 300, 100 );
      this->listView1->TabIndex = 0;

      // Enable editing of the items in the ListView.
      this->listView1->LabelEdit = true;

      // Connect the ListView::ColumnClick event to the ColumnClick event handler.
      this->listView1->ColumnClick += gcnew ColumnClickEventHandler( this, &ListViewSortForm::ColumnClick );

      // Initialize the form.
      this->ClientSize = System::Drawing::Size( 400, 400 );
      array<Control^>^temp5 = {this->listView1};
      this->Controls->AddRange( temp5 );
      this->Name = "ListViewSortForm";
      this->Text = "Sorted ListView Control";

      // Resume lay[Out] of* the form.
      this->ResumeLayout( false );
   }

private:

   // ColumnClick event handler.
   void ColumnClick( Object^ /*o*/, ColumnClickEventArgs^ e )
   {
      // Set the ListViewItemSorter property to a new ListViewItemComparer 
      // object. Setting this property immediately sorts the 
      // ListView using the ListViewItemComparer object.
      this->listView1->ListViewItemSorter = gcnew ListViewItemComparer( e->Column );
   }
};

[System::STAThreadAttribute]
int main()
{
   Application::Run( gcnew ListViewSortForm );
}
package ListViewSortFormNamespace;

import System.*;
import System.Windows.Forms.*;
import System.Drawing.*;
import System.Collections.*;

public class ListViewSortForm extends Form
{
    private ListView listView1;

    public ListViewSortForm()
    {
        // Create ListView items to add to the control.
        ListViewItem listViewItem1 = new ListViewItem(new String[] { "Banana", 
            "a", "b", "c" }, -1, Color.Empty, Color.get_Yellow(), null);
        ListViewItem listViewItem2 = new ListViewItem(new String[] { "Cherry", 
            "v", "g", "t" }, -1, Color.Empty, Color.get_Red(), 
            new Font("Microsoft Sans Serif", (float)8.25, FontStyle.Regular, 
            GraphicsUnit.Point, (ubyte)0));
        ListViewItem listViewItem3 = new ListViewItem(new String[] { "Apple", 
            "h", "j", "n" }, -1, Color.Empty, Color.get_Lime(), null);
        ListViewItem listViewItem4 = new ListViewItem(new String[] { "Pear", 
            "y", "u", "i" }, -1, Color.Empty, Color.FromArgb(192, 128, 156), 
            null);

        //Initialize the ListView control and add columns to it.
        this.listView1 = new ListView();

        // Set the initial sorting type for the ListView.
        this.listView1.set_Sorting(SortOrder.None);

        // Disable automatic sorting to enable manual sorting.
        this.listView1.set_View(View.Details);

        // Add columns and set their text.
        this.listView1.get_Columns().Add(new ColumnHeader());
        this.listView1.get_Columns().get_Item(0).set_Text("Column 1");
        this.listView1.get_Columns().get_Item(0).set_Width(100);
        listView1.get_Columns().Add(new ColumnHeader());
        listView1.get_Columns().get_Item(1).set_Text("Column 2");
        listView1.get_Columns().Add(new ColumnHeader());
        listView1.get_Columns().get_Item(2).set_Text("Column 3");
        listView1.get_Columns().Add(new ColumnHeader());
        listView1.get_Columns().get_Item(3).set_Text("Column 4");

        // Suspend control logic until form is done configuring form.
        this.SuspendLayout();

        // Add Items to the ListView control.
        this.listView1.get_Items().AddRange(new ListViewItem[] { listViewItem1,
            listViewItem2, listViewItem3, listViewItem4 });

        // Set the location and size of the ListView control.
        this.listView1.set_Location(new Point(10, 10));
        this.listView1.set_Name("listView1");
        this.listView1.set_Size(new Size(300, 100));
        this.listView1.set_TabIndex(0);

        // Enable editing of the items in the ListView.
        this.listView1.set_LabelEdit(true);

        // Connect the ListView.ColumnClick event to the 
        // ColumnClick event handler.
        this.listView1.add_ColumnClick(new ColumnClickEventHandler(ColumnClick));

        // Initialize the form.
        this.set_ClientSize(new Size(400, 400));
        this.get_Controls().AddRange(new Control[] { this.listView1 });
        this.set_Name("ListViewSortForm");
        this.set_Text("Sorted ListView Control");

        // Resume layout of the form.
        this.ResumeLayout(false);
    } //ListViewSortForm

    // ColumnClick event handler.
    private void ColumnClick(Object o, ColumnClickEventArgs e)
    {
        // Set the ListViewItemSorter property to a new ListViewItemComparer 
        // object. Setting this property immediately sorts the 
        // ListView using the ListViewItemComparer object.
        this.listView1.set_ListViewItemSorter(
            new ListViewItemComparer(e.get_Column()));
    } //ColumnClick

    /** @attribute System.STAThreadAttribute()
     */
    public static void main(String[] args)
    {
        Application.Run(new ListViewSortForm());
    } //main
} //ListViewSortForm

// Implements the manual sorting of items by columns.
class ListViewItemComparer implements IComparer
{
    private int col;

    public ListViewItemComparer()
    {
        col = 0;
    } //ListViewItemComparer

    public ListViewItemComparer(int column)
    {
        col = column;
    } //ListViewItemComparer

    public int Compare(Object x, Object y)
    {
        return String.Compare(
            ((ListViewItem)x).get_SubItems().get_Item(col).get_Text(),
            ((ListViewItem)y).get_SubItems().get_Item(col).get_Text());
    } //Compare
} //ListViewItemComparer

继承层次结构

System.Object
  System.Windows.Forms.ListView.ColumnHeaderCollection

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

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 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

ListView.ColumnHeaderCollection 成员
System.Windows.Forms 命名空间