次の方法で共有


ColumnHeader クラス

ListView コントロール内の単一の列ヘッダー見出しを表示します。

この型のすべてのメンバの一覧については、ColumnHeader メンバ を参照してください。

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Windows.Forms.ColumnHeader

Public Class ColumnHeader
   Inherits Component
   Implements ICloneable
[C#]
public class ColumnHeader : Component, ICloneable
[C++]
public __gc class ColumnHeader : public Component, ICloneable
[JScript]
public class ColumnHeader extends Component implements ICloneable

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

列ヘッダーは、 ListView コントロール内の項目で、見出しのテキストを格納します。 ColumnHeader オブジェクトは、 ListView.ColumnHeaderCollection クラスの Add メソッドを使用して、 ListView に追加できます。 ListView に列のグループを追加するには、 ListView.ColumnHeaderCollection クラスの AddRange メソッドを使用できます。 ColumnHeader クラスの Index プロパティを使用して、 ColumnHeaderListView.ColumnHeaderCollection 内で格納されている場所を確認できます。

ColumnHeader は、 Text プロパティおよび TextAlign プロパティを提供し、コントロールに表示されるテキストと列ヘッダーのテキストの配置を設定します。 ColumnHeaderListView コントロールに関連付けられているかどうかを確認するには、 ListView プロパティを参照します。別の ListView コントロール内で使用するために ColumnHeader をコピーする場合は、 Clone メソッドを使用します。

使用例

[Visual Basic, C#] ListView コントロールを初期化するコード例を次に示します。このコードは、ColumnHeader オブジェクトを作成し、 Text プロパティ、 TextAlign プロパティ、および Width プロパティを設定します。このコードはまた、ListView に項目とサブ項目を追加します。この例を実行するには、次のコードをフォームに貼り付けて、フォームのコンストラクタまたは Load メソッドで PopulateListView メソッドを呼び出します。

 
Private Sub PopulateListView()
    ListView1.Width = 270
    ListView1.Location = New System.Drawing.Point(10, 10)

    ' Declare and construct the ColumnHeader objects.
    Dim header1, header2 As ColumnHeader
    header1 = New ColumnHeader
    header2 = New ColumnHeader

    ' Set the text, alignment and width for each column header.
    header1.Text = "File name"
    header1.TextAlign = HorizontalAlignment.Left
    header1.Width = 70

    header2.TextAlign = HorizontalAlignment.Left
    header2.Text = "Location"
    header2.Width = 200

    ' Add the headers to the ListView control.
    ListView1.Columns.Add(header1)
    ListView1.Columns.Add(header2)

    ' Populate the ListView.Items property.
    ' Set the directory to the sample picture directory.
    Dim dirInfo As New System.IO.DirectoryInfo _
        ("C:\Documents and Settings\All Users" _
        & "\Documents\My Pictures\Sample Pictures")
    Dim file As System.IO.FileInfo

    ' Get the .jpg files from the directory
    Dim files() As System.io.FileInfo = dirInfo.GetFiles("*.jpg")

    ' Add each file name and full name including path
    ' to the ListView.
    If Not (files Is Nothing) Then
        For Each file In files
            Dim item As New ListViewItem(file.Name)
            item.SubItems.Add(file.FullName)
            ListView1.Items.Add(item)
        Next
    End If
End Sub

[C#] 
private void PopulateListView()
{
    ListView1.Width = 270;
    ListView1.Location = new System.Drawing.Point(10, 10);

    // Declare and construct the ColumnHeader objects.
    ColumnHeader header1, header2;
    header1 = new ColumnHeader();
    header2 = new ColumnHeader();

    // Set the text, alignment and width for each column header.
    header1.Text = "File name";
    header1.TextAlign = HorizontalAlignment.Left;
    header1.Width = 70;

    header2.TextAlign = HorizontalAlignment.Left;
    header2.Text = "Location";
    header2.Width = 200;

    // Add the headers to the ListView control.
    ListView1.Columns.Add(header1);
    ListView1.Columns.Add(header2);

    // Populate the ListView.Items property.
    // Set the directory to the sample picture directory.
    System.IO.DirectoryInfo dirInfo = 
        new System.IO.DirectoryInfo(
        "C:\\Documents and Settings\\All Users" +
        "\\Documents\\My Pictures\\Sample Pictures");
    

    // Get the .jpg files from the directory
    System.IO.FileInfo[] files = dirInfo.GetFiles("*.jpg");

    // Add each file name and full name including path
    // to the ListView.
    if (files != null)
    {
        foreach ( System.IO.FileInfo file in files )
        {
            ListViewItem item = new ListViewItem(file.Name);
            item.SubItems.Add(file.FullName);
            ListView1.Items.Add(item);
        }
    }
}

[C++, JScript] C++ および JScript のサンプルはありません。Visual Basic および C# のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Windows.Forms

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

アセンブリ: System.Windows.Forms (System.Windows.Forms.dll 内)

参照

ColumnHeader メンバ | System.Windows.Forms 名前空間 | ListView | ListView.ColumnHeaderCollection