共用方式為


ListView.AutoResizeColumn(Int32, ColumnHeaderAutoResizeStyle) 方法

定義

根據調整大小樣式的指示,重新調整指定之資料行的寬度。

public:
 void AutoResizeColumn(int columnIndex, System::Windows::Forms::ColumnHeaderAutoResizeStyle headerAutoResize);
public void AutoResizeColumn (int columnIndex, System.Windows.Forms.ColumnHeaderAutoResizeStyle headerAutoResize);
member this.AutoResizeColumn : int * System.Windows.Forms.ColumnHeaderAutoResizeStyle -> unit
Public Sub AutoResizeColumn (columnIndex As Integer, headerAutoResize As ColumnHeaderAutoResizeStyle)

參數

columnIndex
Int32

要調整大小的資料行之以零為起始的索引。

headerAutoResize
ColumnHeaderAutoResizeStyle

其中一個 ColumnHeaderAutoResizeStyle 值。

例外狀況

Columnsnull 時,columnIndex 大於 0

-或-

columnIndex 小於 0 或大於資料行設定的數目。

headerAutoResize 不是 ColumnHeaderAutoResizeStyle 列舉的成員。

範例

下列範例會 ListView 初始化 詳細檢視,並使用 AutoResizeColumn 方法自動調整資料行的大小。 若要執行此範例,請將此程式碼貼到 Windows Form 中,並從表單的建構函式或 Load 事件處理常式呼叫 InitializeResizingListView 方法。

private ListView resizingListView = new ListView();
private Button button1 = new Button();

private void InitializeResizingListView()
{
    // Set location and text for button.
    button1.Location = new Point(100, 15);
    button1.Text = "Resize";
    button1.Click += new EventHandler(button1_Click);

    // Set the ListView to details view.
    resizingListView.View = View.Details;

    //Set size, location and populate the ListView.
    resizingListView.Size = new Size(200, 100);
    resizingListView.Location = new Point(40, 40);
    resizingListView.Columns.Add("HeaderSize");
    resizingListView.Columns.Add("ColumnContent");
    ListViewItem listItem1 = new ListViewItem("Short");
    ListViewItem listItem2 = new ListViewItem("Tiny");
    listItem1.SubItems.Add(new ListViewItem.ListViewSubItem( 
            listItem1, "Something longer"));
    listItem2.SubItems.Add(new ListViewItem.ListViewSubItem(
        listItem2, "Something even longer"));
    resizingListView.Items.Add(listItem1);
    resizingListView.Items.Add(listItem2);

    // Add the ListView and the Button to the form.
    this.Controls.Add(resizingListView);
    this.Controls.Add(button1);
}

private void button1_Click(object sender, EventArgs e)
{
    resizingListView.AutoResizeColumn(0, 
        ColumnHeaderAutoResizeStyle.HeaderSize);
    resizingListView.AutoResizeColumn(1, 
        ColumnHeaderAutoResizeStyle.ColumnContent);
}
Private resizingListView As New ListView()
Private WithEvents button1 As New Button()


Private Sub InitializeResizingListView()
    ' Set location and text for button.
    button1.Location = New Point(100, 15)
    button1.Text = "Resize"
    AddHandler button1.Click, AddressOf button1_Click

    ' Set the ListView to details view.
    resizingListView.View = View.Details

    'Set size, location and populate the ListView.
    resizingListView.Size = New Size(200, 100)
    resizingListView.Location = New Point(40, 40)
    resizingListView.Columns.Add("HeaderSize")
    resizingListView.Columns.Add("ColumnContent")
    Dim listItem1 As New ListViewItem("Short")
    Dim listItem2 As New ListViewItem("Tiny")
    listItem1.SubItems.Add(New ListViewItem.ListViewSubItem(listItem1, _
        "Something longer"))
    listItem2.SubItems.Add(New ListViewItem.ListViewSubItem(listItem2, _
        "Something even longer"))
    resizingListView.Items.Add(listItem1)
    resizingListView.Items.Add(listItem2)

    ' Add the ListView and the Button to the form.
    Me.Controls.Add(resizingListView)
    Me.Controls.Add(button1)

End Sub


Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
    Handles button1.Click

    ' Resize the first column to the header size.
    resizingListView.AutoResizeColumn(0, _
        ColumnHeaderAutoResizeStyle.HeaderSize)

    ' Resize the second column to the column content.
    resizingListView.AutoResizeColumn(1, _
        ColumnHeaderAutoResizeStyle.ColumnContent)

End Sub

備註

只有在建構 和 包含 Form 之後 ListView ,呼叫這個方法才有效,而且資料 ListView 行標頭和資料行會填入專案。 如果將新專案新增至 ListView ,除非再次呼叫 ,否則 AutoResizeColumn 資料行將不會調整大小。

適用於