阻止用户调整ListView中列宽的大小? [英] Preventing users from resizing columns widths in ListView?

查看:46
本文介绍了阻止用户调整ListView中列宽的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Winform中有一个ListView,其中有4个列,分别是名称,资金,ID和级别.

I have a ListView In My Winform that has 4columns, Name, Money, ID and Level.

问题是,当我运行我的应用程序时,我仍然能够弄乱列的宽度并更改它们.

The problem is when I run my app, I still have the ability to mess with the columns widths and change them.

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

I searched And found that I should do something like this:

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

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

But the problem is that when I debugged and Ccanged the columns widths, this event didn't even fire!

为什么不开火?

如何固定列宽?

我制作了一个新的winform应用,以防万一我的旧版出现问题,它被触发了,但仅是第一次运行该应用程序..这是代码:

I made a new winform app just in case if there was something wrong in my old one, it fired, but only for the first time running the app .. here's the code:

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;
    }
  }
}

这是初始化组件,以防万一您想知道的情况:

here is the initialize component just in case you might wanna know:

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);

}

推荐答案

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

You may check Better ListView Express. We have implemented AllowResize property on the columns, which does exactly what you need.

这篇关于阻止用户调整ListView中列宽的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆