ListView.ColumnHeaderCollection 類別

定義

代表 ListView 控制項中的資料行標頭集合。

public: ref class ListView::ColumnHeaderCollection : System::Collections::IList
public class ListView.ColumnHeaderCollection : System.Collections.IList
[System.ComponentModel.ListBindable(false)]
public class ListView.ColumnHeaderCollection : System.Collections.IList
type ListView.ColumnHeaderCollection = class
    interface IList
    interface ICollection
    interface IEnumerable
[<System.ComponentModel.ListBindable(false)>]
type ListView.ColumnHeaderCollection = class
    interface IList
    interface ICollection
    interface IEnumerable
Public Class ListView.ColumnHeaderCollection
Implements IList
繼承
ListView.ColumnHeaderCollection
屬性
實作

範例

下列程式碼範例會建立一個 ListView 表單,其中包含按一下控制項中的資料 ListView 行時,手動排序專案的控制項。 此範例會定義稱為 ListViewItemComparer 的類別,這個類別會實 System.Collections.IComparer 作執行比較的 ListViewItem 介面。 此範例會建立 的 ListViewItemComparer 實例,並使用它來設定 ListViewItemSorter 控制項的 ListView 屬性。 Sort事件處理常式中的 ColumnClick 方法呼叫會使用 中 ListViewItemComparer 定義的方法,根據所按一下的資料行來執行專案排序。

#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 );
}
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);
        }
    }
}
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


        ' 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

備註

當 屬性設定 Details 為 時 View ,類別 ListView.ColumnHeaderCollection 會儲存控制項中顯示的 ListView 資料行標頭。 會 ListView.ColumnHeaderCollection 儲存 ColumnHeader 物件,這些物件會定義要顯示資料行的文字,以及顯示資料行時,資料行標頭在控制項中的 ListView 顯示方式。 ListView當顯示資料行時,專案及其子專案會顯示在自己的資料行中。 若要指定顯示哪些資料行子專案資料,請參閱 ListViewItem.ListViewSubItemCollection 類別。

有數種方式可將資料行標頭新增至集合。 方法會將 Add 單一資料行標頭新增至集合。 若要將數個數據行標頭加入集合中,您可以建立 物件的陣列 ColumnHeader ,並將其傳遞至 AddRange 方法。 如果您想要在集合中的特定位置插入資料行標頭,您可以使用 Insert 方法。 若要移除資料行標頭,如果您知道資料行標頭位於集合中的位置,可以使用 Remove 方法或 RemoveAt 方法。 方法 Clear 可讓您從集合中移除所有資料行標頭,而不是使用 Remove 方法一次移除單一資料行標頭。

除了新增和移除資料行標頭的方法和屬性之外, ListView.ColumnHeaderCollection 也提供方法來尋找集合中的資料行標頭。 方法 Contains 可讓您判斷資料行標頭是否為集合的成員。 一旦您知道資料行標頭位於集合中,您就可以使用 IndexOf 方法來判斷資料行標頭位於集合中的位置。

注意

當所有資料行的合併寬度超過 32,768 圖元時,可能會產生非預期的行為。

建構函式

ListView.ColumnHeaderCollection(ListView)

初始化 ListView.ColumnHeaderCollection 類別的新執行個體。

屬性

Count

取得集合中的項目數目。

IsReadOnly

取得值,表示集合是否為唯讀。

Item[Int32]

取得集合中位於指定索引處的資料行行首。

Item[String]

從集合中取得具有指定索引鍵的資料行行首。

方法

Add(ColumnHeader)

將現有的 ColumnHeader 加入至集合。

Add(String)

建立具有指定之文字的資料行,並將它加入集合中。

Add(String, Int32)

建立具有指定之文字和寬度的資料行,並將它加入集合中。

Add(String, Int32, HorizontalAlignment)

使用指定文字、寬度和對齊設定,將資料行行首加入至集合中。

Add(String, String)

建立具有指定之文字和索引鍵的資料行,並將它加入集合中。

Add(String, String, Int32)

建立具有指定之文字、索引鍵和寬度的資料行,並將它加入集合中。

Add(String, String, Int32, HorizontalAlignment, Int32)

建立具有指定之索引鍵、對齊文字、寬度和影像索引的資料行,並將它加入集合中。

Add(String, String, Int32, HorizontalAlignment, String)

建立具有指定之索引鍵、對齊文字、寬度和影像索引鍵的資料行,並將它加入集合中。

AddRange(ColumnHeader[])

將資料行行首的陣列加入集合。

Clear()

從集合移除所有資料行行首。

Contains(ColumnHeader)

判斷指定的資料行行首是否位於集合中。

ContainsKey(String)

判斷具有指定索引鍵的資料行是否包含在集合中。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetEnumerator()

傳回用於逐一查看資料行行首集合的列舉值。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IndexOf(ColumnHeader)

傳回集合中指定之資料行行首的索引。

IndexOfKey(String)

判斷具有指定索引鍵之資料行的索引。

Insert(Int32, ColumnHeader)

將現有的資料行行首插入位於指定索引處的集合中。

Insert(Int32, String)

使用指定的文字,建立新的資料行行首,並將此行首插入集合中的指定索引處。

Insert(Int32, String, Int32)

使用指定的文字和初始寬度,建立新的資料行行首,並將此行首插入集合中的指定索引處。

Insert(Int32, String, Int32, HorizontalAlignment)

建立新的資料行行首,並且將它插入位於指定索引處的集合中。

Insert(Int32, String, String)

使用指定的文字和索引鍵,建立新的資料行行首,並將此行首插入集合中的指定索引處。

Insert(Int32, String, String, Int32)

使用指定的文字、索引鍵和寬度,建立新的資料行行首,並將此行首插入集合中的指定索引處。

Insert(Int32, String, String, Int32, HorizontalAlignment, Int32)

使用指定的對齊文字、索引鍵、寬度和影像索引,建立新的資料行行首,並將此行首插入集合中的指定索引處。

Insert(Int32, String, String, Int32, HorizontalAlignment, String)

使用指定的對齊文字、索引鍵、寬度和影像索引鍵,建立新的資料行行首,並將此行首插入集合中的指定索引處。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Remove(ColumnHeader)

從集合中移除指定資料行行首。

RemoveAt(Int32)

移除集合中位於指定索引處的資料行行首。

RemoveByKey(String)

從集合中移除具有指定索引鍵的資料行。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

ICollection.CopyTo(Array, Int32)

從特定的陣列索引開始,將 ColumnHeader 中的 ListView.ColumnHeaderCollection 物件複製到陣列中。

ICollection.IsSynchronized

取得值,這個值表示對 ListView.ColumnHeaderCollection 的存取是否同步 (安全執行緒)。

ICollection.SyncRoot

取得可用來對控制項集合進行同步 (Synchronize) 存取的物件。

IList.Add(Object)

ColumnHeader 加入至 ListView

IList.Contains(Object)

判斷指定的資料行行首是否位於集合中。

IList.IndexOf(Object)

此 API 支援此產品基礎結構,但無法直接用於程式碼之中。

傳回集合中指定之資料行行首的索引。

IList.Insert(Int32, Object)

將現有的資料行行首插入位於指定索引處的集合中。

IList.IsFixedSize

取得值,指出 ListView.ColumnHeaderCollection 是否有固定的大小。

IList.Item[Int32]

取得或設定在集合中位於指定索引處的資料行行首。

IList.Remove(Object)

從集合中移除指定資料行行首。

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於