C# listview双击鼠标修改单元格内容

1.C#创建winform工程;

2.在界面添加工具listview1;

3.鼠标右键点击listview1,更改属性:

FullRowSelect=True     开启行选择模式,开启才可以进行双击出第一列的其他列

HeaderStyle=Nonclickable   不响应鼠标单击列头操作

MultiSelect=True     允许选择多项

view=Details

4.初始化listview1,添加表头

 public void InitList()
        {
            ImageList imglist = new ImageList();
            imglist.ImageSize = new Size(1, 20);
            listView1.SmallImageList = imglist;

            listView1.View = View.Details;
            this.listView1.Columns.Add("序号", 40, HorizontalAlignment.Left);
            this.listView1.Columns.Add("列表1", 120, HorizontalAlignment.Left);
            this.listView1.Columns.Add("列表2", listView1.Width - 185, HorizontalAlignment.Left);
        }

5.添加函数接口和自定义textbox控件

        [DllImport("user32")]
        public static extern int GetScrollPos(int hwnd, int nBar);
        private TextBox txtInput;

6.选择listview1事件:

添加鼠标双击事件

        private void listView1_MouseDoubleClick(object sender, MouseEventArgs 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 - 2)//第一列为序号,不修改。如果双击为第一列则不可以进入修改
                {
                    return;
                }

                this.txtInput = new TextBox();

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

                //begin edit
                if (item != null)
                {
                    nDClick = this.listView1.Items[listView1.SelectedIndices[0]].Index;
                    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;

                        //检查PN是否存在
                        int Index = 0;
                        if (CheckPnIsExist(this.txtInput.Text, ref Index))
                        {
                            this.txtInput.Text = "";
                            MessageBox.Show("第" + (Index + 1) + "行已经存在相同PN号,请重新输入!");
                        }
                        else
                        {
                            lvst.Text = this.txtInput.Text;
                        }

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

            }
        }

        //添加事件SelectedIndexChanged,释放文本框内容
        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;
                       //检查PN是否存在
                        int Index = 0;
                        if (CheckPnIsExist(this.txtInput.Text,ref Index))
                        {
                            this.txtInput.Text = "";
                            MessageBox.Show("第"+(Index+1)+"行已经存在相同PN号,请重新输入!");
                        }
                        else
                        {
                            lvst.Text = this.txtInput.Text;
                        }
                        
                    }

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

            }
        }

//添加事件,鼠标单击MouseClick,将文本框内容显示在listview1

    private void listView1_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                if (this.txtInput != null)
                {
                    if (this.txtInput.Text.Length > 0)
                    {
                        ListViewItem.ListViewSubItem lvst = (ListViewItem.ListViewSubItem)this.txtInput.Tag;

                        //检查PN是否存在
                        int Index = 0;
                        if (CheckPnIsExist(this.txtInput.Text, ref Index))
                        {
                            this.txtInput.Text="";
                            MessageBox.Show("第" + (Index + 1) + "行已经存在相同PN号,请重新输入!");
                        }
                        else
                        {
                            lvst.Text = this.txtInput.Text;
                        }
                    }

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

            }
        }

猜你喜欢

转载自blog.csdn.net/Hat_man_/article/details/108246877