日期:2014-05-19  浏览次数:20831 次

鼠标拖动改变listview的columnHeader的width时怎么触发事件?


------解决方案--------------------
下面的代码示例演示如何处理 ColumnWidthChanging 事件。它还演示了 ColumnWidthChangingEventArgs.NewWidth 和 Cancel 成员。要运行此示例,请将这段代码粘贴到一个 Windows 窗体中。从窗体的构造函数或 Load 事件处理程序调用 InitializeListView1。

ListView listView1 = new ListView();
private void InitializeListView1()
{
// Initialize a ListView in detail view and add some columns.
listView1.View = View.Details;
listView1.Width = 200;
listView1.Columns.Add( "Column1 ");
listView1.Columns.Add( "Column2 ");

// Associate a method with the ColumnWidthChangingEvent.
listView1.ColumnWidthChanging +=
new ColumnWidthChangingEventHandler(listView1_ColumnWidthChanging);
this.Controls.Add(listView1);
}

// Handle the ColumnWidthChangingEvent.
private void listView1_ColumnWidthChanging(object sender,
ColumnWidthChangingEventArgs e)
{
// Check if the new width is too big or too small.
if (e.NewWidth > 100 || e.NewWidth < 5)
{
// Cancel the event and inform the user if the new
// width does not meet the criteria.
MessageBox.Show( "Column width is too large or too small ");
e.Cancel = true;
}
}