sumitrapaul123 0 Newbie Poster

Hi,

I have to make listview editable. I am doing it on BeforeLabelEdit event.
I want to do the same thing on double click of an item.

How to invoke BeforeLabelEdit event on listview item double click??

Can someone help me on this. Please see my code below.`

public partial class Form1 : Form
    {
        private System.Windows.Forms.ListView listView1;
        private System.Windows.Forms.Button Add;
        private System.Windows.Forms.ColumnHeader PresetView;
        private System.Windows.Forms.ColumnHeader columnHeader1;
        public Form1()
        {
            InitializeComponent();
            Init();
            listView1.LabelEdit = true;
            listView1.BeforeLabelEdit += lvw_BeforeLabelEdit;
            editBox = new TextBox();
            editBox.Name = "eBox";
        }

        private void Init()
        {
            this.listView1 = new System.Windows.Forms.ListView();
            this.PresetView = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.Add = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // listView1
            // 
            this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.PresetView,
            this.columnHeader1});
            this.listView1.FullRowSelect = true;
            this.listView1.Location = new System.Drawing.Point(0, 0);
            this.listView1.Name = "listView1";
            this.listView1.Size = new System.Drawing.Size(355, 142);
            this.listView1.TabIndex = 0;
            this.listView1.UseCompatibleStateImageBehavior = false;
            this.listView1.View = System.Windows.Forms.View.Details;
            // 
            // PresetView
            // 
            this.PresetView.Text = "Viewing Preset";
            this.PresetView.Width = 172;
            // 
            // columnHeader1
            // 
            this.columnHeader1.Text = "Matching String";
            this.columnHeader1.Width = 171;
            // 
            // Add
            // 
            this.Add.Location = new System.Drawing.Point(361, 72);
            this.Add.Name = "Add";
            this.Add.Size = new System.Drawing.Size(75, 23);
            this.Add.TabIndex = 1;
            this.Add.Text = "Add";
            this.Add.UseVisualStyleBackColor = true;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(460, 154);
            this.Controls.Add(this.Add);
            this.Controls.Add(this.listView1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
        }



        private TextBox editBox;
        private ListViewItem sItem;

        private void lvw_BeforeLabelEdit(object sender, LabelEditEventArgs e)
        {
            sItem = listView1.SelectedItems[0];
            int y = sItem.Bounds.Y;
            int h = sItem.Bounds.Height;
            int x1 = sItem.Bounds.Width - listView1.Columns[1].Width;
            int xw = listView1.Columns[1].Width;
            editBox.Width = xw;
            editBox.Height = h;
            editBox.Font = new Font(editBox.Font.Name, 8);
            editBox.Margin = new Padding(0, 0, 0, 0);
            editBox.Location = new Point(x1, y);
            this.Controls.Add(editBox);
            editBox.Focus();
            editBox.Text = sItem.SubItems[1].Text;
            editBox.LostFocus += new EventHandler(editBox_LostFocus);
        }

        void editBox_LostFocus(object sender, EventArgs e)
        {
            sItem.SubItems[1].Text = editBox.Text;
            this.Controls.Remove(editBox);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            AddListViewItems();
        }

        private void AddListViewItems()
        {
            ListViewItem item1 = new ListViewItem("Something");
            item1.SubItems.Add("SubItem1a");
            listView1.Items.Add(item1);

            ListViewItem item2 = new ListViewItem("Something2");
            item2.SubItems.Add("SubItem2a");
            listView1.Items.Add(item2);

            ListViewItem item3 = new ListViewItem("Something3");
            item3.SubItems.Add("SubItem3a");
            listView1.Items.Add(item3);

            ListViewItem item4 = new ListViewItem("Something");
            item4.SubItems.Add("SubItem1a");
            listView1.Items.Add(item4);

            ListViewItem item5 = new ListViewItem("Something2");
            item5.SubItems.Add("SubItem2a");
            listView1.Items.Add(item5);

            ListViewItem item6 = new ListViewItem("Something3");
            item6.SubItems.Add("SubItem3a");
            listView1.Items.Add(item6);
        }
    }

Regards

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.