防止用户调整 ListView 中的列宽?

问题描述 投票:0回答:4

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

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

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

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

但问题是,当我调试并取消列宽时,该事件甚至没有触发!

为什么不火?

如何固定列宽?

我制作了一个新的 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);

}
c# winforms listview
4个回答
15
投票

您需要在表单中注册 ColumnWidthChanging 事件:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        // This line registers the event, soc that the form can "hear" it and call the indicated handling code:
        this.listView1.ColumnWidthChanging += new ColumnWidthChangingEventHandler(listView1_ColumnWidthChanging);
    }

    void listView1_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
    {
        Console.Write("Column Resizing");
        e.NewWidth = this.listView1.Columns[e.ColumnIndex].Width;
        e.Cancel = true;
    }
}

6
投票

只需单击属性>>事件>>列宽度更改

然后添加这段代码:

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

编码愉快! ^_^


0
投票

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


0
投票

如果这里有人只想限制网格上的某些列,请使用此代码。

        {
            if (listView.Columns[e.ColumnIndex].Index == 3 || listView.Columns[e.ColumnIndex].Index == 4 || listView.Columns[e.ColumnIndex].Index == 5 || listView.Columns[e.ColumnIndex].Index == 6)

            {
                //Do nothihng
            }
       
            else
            {
                e.Cancel = true;
                e.NewWidth = lvwItems.Columns[e.ColumnIndex].Width;
            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.