listview 双击item进入编辑

   #region listview 双击item进入编辑
        //获取鼠标点击的项------API
        [DllImport("user32")]
        public static extern int GetScrollPos(int hwnd, int nBar);

        private TextBox txtInput;

        //获取点击项的位置
        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {
                Point point = new Point(e.X, e.Y);
                ItemPoint(point);
            }
            catch (Exception ex)
            {

            }
        }
        //获取项的位置
        public void ItemPoint(Point e)
        {
            try
            {

                ListViewItem item = this.listView1.GetItemAt(e.X, e.Y);

                //找到文本框
                Rectangle rect = item.GetBounds(ItemBoundsPortion.Entire);
                int StartX = rect.Left; //获取文本框位置的X坐标
                int ColumnIndex = 0;    //文本框的索引

                //获取列的索引
                //得到滑块的位置
                int pos = GetScrollPos(this.listView1.Handle.ToInt32(), 0);
                foreach (ColumnHeader Column in listView1.Columns)
                {
                    if (e.X + pos >= StartX + Column.Width)
                    {
                        StartX += Column.Width;
                        ColumnIndex += 1;
                    }
                }

                if (ColumnIndex < this.listView1.Columns.Count - 1)
                {
                    return;
                }

                this.txtInput = new TextBox();

                //locate the txtinput and hide it. txtInput为TextBox
                this.txtInput.Parent = this.listView1;

                //begin edit
                if (item != null)
                {
                    rect.X = StartX;
                    rect.Width = this.listView1.Columns[ColumnIndex].Width; //得到长度和ListView的列的长度相同                    
                    this.txtInput.Bounds = rect;
                    this.txtInput.Multiline = true;
                    //显示文本框
                    this.txtInput.Text = item.SubItems[ColumnIndex].Text;
                    this.txtInput.Tag = item.SubItems[ColumnIndex];
                    this.txtInput.KeyPress += new KeyPressEventHandler(txtInput_KeyPress);
                    this.txtInput.Focus();
                }
            }
            catch (Exception ex)
            {

            }
        }
        //回车保存内容
        private void txtInput_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                if ((int)e.KeyChar == 13)
                {
                    if (this.txtInput != null)
                    {
                        ListViewItem.ListViewSubItem lvst = (ListViewItem.ListViewSubItem)this.txtInput.Tag;

                        lvst.Text = this.txtInput.Text;

                        this.txtInput.Dispose();
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

        //移动焦点,释放保存文本框内容
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.txtInput != null)
                {
                    if (this.txtInput.Text.Length > 0)
                    {
                        ListViewItem.ListViewSubItem lvst = (ListViewItem.ListViewSubItem)this.txtInput.Tag;

                        lvst.Text = this.txtInput.Text;
                    }

                    this.txtInput.Dispose();
                }
            }
            catch (Exception ex)
            {

            }
        }



        #endregion
原文地址:https://www.cnblogs.com/xuezhu/p/14889575.html