次の方法で共有


ColumnHeader.TextAlign プロパティ

ColumnHeader で表示されたテキストの水平方向の配置を取得または設定します。

Public Property TextAlign As HorizontalAlignment
[C#]
public HorizontalAlignment TextAlign {get; set;}
[C++]
public: __property HorizontalAlignment get_TextAlign();public: __property void set_TextAlign(HorizontalAlignment);
[JScript]
public function get TextAlign() : HorizontalAlignment;public function set TextAlign(HorizontalAlignment);

プロパティ値

HorizontalAlignment 値の 1 つ。既定値は HorizontalAlignment.Left です。

解説

このプロパティを使用して、各 ColumnHeader に表示されたテキストに対して異なる配置を設定できます。たとえば、 ListView コントロールの最初の ColumnHeader を左側に配置し、残りのフィールドを中央または右側に配置できます。

使用例

[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# のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: 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

参照

ColumnHeader クラス | ColumnHeader メンバ | System.Windows.Forms 名前空間 | HorizontalAlignment