Preventing users from resizing columns widths in ListView?
Asked Answered
P

4

9

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!

Why didn't it fire?

And how can I make the column widths fixed?

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

}
Pillory answered 25/7, 2012 at 18:55 Comment(2)
possible duplicate of how to fix the column width of a listview in c# windows form?Sayers
it fires. Try ensuring the event is being registered properlyDearth
C
0

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

Cowage answered 4/8, 2012 at 0:34 Comment(0)
R
15

You need to register the ColumnWidthChanging event with your form:

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;
    }
}
Rehearse answered 25/7, 2012 at 19:21 Comment(4)
I mean, the rest of your Form code. Obviously, there is something going on which is preventing the event from occurring normally.Rehearse
Very sorry about the delayed answers .. You were right, there was some problem in my code, I made a new winform project, just with a listview inside, just to check if the event was firing, at first, it fired, but then it didn't .. (only the first time I ran the program it fired)Pillory
Try setting e.Cancel = true AFTER you set the e.NewWidth ValueRehearse
@Rehearse this works perfectly and that's what I'am looking for.Euhemerize
A
7

Just click on the Properties>>Events>>ColumnWidthChanging.

enter image description here

Then add this code:

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

Happy coding! ^_^

Afternoons answered 7/6, 2017 at 9:56 Comment(3)
I'm surprised cancelling doesn't just prevent the change anyway - do you need the second line?Cons
yes sir, by getting the current Width of the listView column and passing it as NewWidth, we are ALWAYS setting the width to the current width. By the way, BRIDGE, is the Application Name of my current program... ^_^Afternoons
Don't worry it hasn't grown sentient, I am a person!Cons
C
0

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

Cowage answered 4/8, 2012 at 0:34 Comment(0)
W
0

If some one here would like to restrict only some colums on the grid, use this code.

        {
            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;
            }
        }
Wurth answered 9/11, 2023 at 6:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.