c# – 阻止用户调整ListView中列宽的大小?

我有一个ListView在我的Winform中有4列,名称,金钱,ID和级别.

问题是当我运行我的应用程序时,我仍然有能力搞乱列宽
并改变它们.

搜索并发现我应该这样做:

private void listView1_ColumnWidthChanging(object sender,ColumnWidthChangingEventArgs e)
{
  e.Cancel = true;
  e.NewWidth = listView1.Columns[e.ColumnIndex].Width;
}

但问题是,当我调试和Ccanged列宽度时,这个事件甚至没有发射!

它为什么不开火?

我怎样才能使列宽固定?

我做了一个新的winform应用程序,以防我的旧版本出现问题,
它解雇了,但只是第一次运行应用程序..这是代码

namespace CsharpWinformTestingStuff
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
      listView1.ColumnWidthChanging += new ColumnWidthChangingEventHandler(listView1_ColumnWidthChanging);
    }

    void listView1_ColumnWidthChanging(object sender,ColumnWidthChangingEventArgs e)
    {

      e.Cancel = true;
      e.NewWidth = listView1.Columns[e.ColumnIndex].Width;
    }
  }
}

这里是初始化组件,以防您可能想知道:

private void InitializeComponent()
{
  this.listView1 = new System.Windows.Forms.ListView();
  this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
  this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
  this.columnHeader3 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
  this.SuspendLayout();
  // 
  // listView1
  // 
  this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
        this.columnHeader1,this.columnHeader2,this.columnHeader3});
  this.listView1.GridLines = true;
  this.listView1.Location = new System.Drawing.Point(12,12);
  this.listView1.Name = "listView1";
  this.listView1.Size = new System.Drawing.Size(284,275);
  this.listView1.TabIndex = 0;
  this.listView1.UseCompatibleStateImageBehavior = false;
  this.listView1.View = System.Windows.Forms.View.Details;
  // 
  // columnHeader1
  // 
  this.columnHeader1.Text = "Name";
  this.columnHeader1.Width = 97;
  // 
  // columnHeader2
  // 
  this.columnHeader2.Text = "Age";
  this.columnHeader2.Width = 52;
  // 
  // columnHeader3
  // 
  this.columnHeader3.Text = "Email";
  this.columnHeader3.Width = 157;
  // 
  // Form1
  // 
  this.AutoScaleDimensions = new System.Drawing.Sizef(6F,13F);
  this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  this.ClientSize = new System.Drawing.Size(308,299);
  this.Controls.Add(this.listView1);
  this.Name = "Form1";
  this.Text = "Form1";
  this.ResumeLayout(false);

}

解决方法

您可以检查 Better ListView Express.我们已经在列上实现了AllowResize属性,这正是您所需要的.

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...