Listview的columnwidthchanging事件不是火吗?

时间:2014-04-14 12:36:27

标签: c# winforms listview column-width

我尽量不允许在listview中调整大小列,但我不能 我不知道为什么listview的columnwidthchanging事件不是火 我尝试调试,看看从不进入方法listView1_ColumnWidthChanging

在Form1.Designer.cs中:

    partial class Form1
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    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.SuspendLayout();
        // 
        // listView1
        // 
        this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
        this.columnHeader1,
        this.columnHeader2});
        this.listView1.Location = new System.Drawing.Point(13, 103);
        this.listView1.Name = "listView1";
        this.listView1.Size = new System.Drawing.Size(259, 97);
        this.listView1.TabIndex = 0;
        this.listView1.UseCompatibleStateImageBehavior = false;
        this.listView1.View = System.Windows.Forms.View.Details;
        this.listView1.ColumnWidthChanging += new System.Windows.Forms.ColumnWidthChangingEventHandler(this.listView1_ColumnWidthChanging);
        // 
        // columnHeader1
        // 
        this.columnHeader1.Text = "ID";
        this.columnHeader1.Width = 0;
        // 
        // columnHeader2
        // 
        this.columnHeader2.Width = 200;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(284, 262);
        this.Controls.Add(this.listView1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.ListView listView1;
    private System.Windows.Forms.ColumnHeader columnHeader1;
    private System.Windows.Forms.ColumnHeader columnHeader2;
}

在Form1.cs中:

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


    }

    private void listView1_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
    {
        if (e.ColumnIndex == 0)
        {
            e.Cancel = true;
            e.NewWidth = 0x0;
        }
    }

请帮助我!!!!!! T_T

1 个答案:

答案 0 :(得分:0)

看起来你正在使用'隐藏'的第一列来捎带一个值。这正是“Tag”属性的用途。

var listViewItem = listView1.Items.Add(rowData);
listViewItem.Tag = id;

然后您可以稍后检索:

var selectedListViewItem = listView1.Items.SelectedItems[0];
var selectedID = (int)selectedListViewItem.Tag;

Windows中有一些设置会影响ColumnWidthChanging事件的触发 - 以及解决它的方法。但如果你不需要,我建议不要去那里。