ListView.AutoResizeColumn(Int32, ColumnHeaderAutoResizeStyle) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
按照大小调整样式的指示,调整给定列的宽度。
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
例外
headerAutoResize
不是 ColumnHeaderAutoResizeStyle 枚举的一个成员。
示例
以下示例初始化 ListView 详细信息视图,并使用 方法自动调整列 AutoResizeColumn 的大小。 若要运行此示例,请将此代码粘贴到 Windows 窗体中,并从窗体的构造函数或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 列将不会调整大小。