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インスタンスを作成し、それを使用してコントロールの プロパティをListViewItemSorterListView設定します。 イベント ハンドラーのColumnClickメソッド呼び出しではSort、 で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

注釈

クラスはListView.ColumnHeaderCollection、 プロパティが に設定されている場合にコントロールにListView表示される列ヘッダーをViewDetails格納します。 には ListView.ColumnHeaderCollectionColumnHeader 列に表示するテキストを定義するオブジェクトと、列を表示するときにコントロールに列ヘッダーを ListView 表示する方法が格納されます。 が列を ListView 表示すると、項目とそのサブ項目が独自の列に表示されます。 サブ項目データを表示する列を指定するには、 クラスを ListViewItem.ListViewSubItemCollection 参照してください。

列ヘッダーをコレクションに追加するには、いくつかの方法があります。 メソッドは Add 、1 つの列ヘッダーをコレクションに追加します。 コレクションに多数の列ヘッダーを追加するには、オブジェクトの ColumnHeader 配列を作成し、 メソッドに AddRange 渡します。 列ヘッダーをコレクション内の特定の場所に挿入する場合は、 メソッドを Insert 使用できます。 列ヘッダーを削除するには、列ヘッダーが Remove コレクション内の RemoveAt どこにあるかがわかっている場合は、 メソッドまたは メソッドを使用できます。 Clearメソッドを使用すると、 メソッドを使用してRemove一度に 1 つの列ヘッダーを削除する代わりに、コレクションからすべての列ヘッダーを削除できます。

列ヘッダーを追加および削除するためのメソッドとプロパティに加えて、 には、 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

コントロールのコレクションへのアクセスを同期するために使用するオブジェクトを取得します。

IList.Add(Object)

ColumnHeaderListView に追加します。

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)

IEnumerableIQueryable に変換します。

適用対象