我写了一个小工具,主要是显示一个listview 弄成一个表格的模样,然后我想实现双击修改某一格子中的内容,但是现在没法实现,把labeledit 属性设置为true也没有用,,因为内容是从网页上抓取的,而不是从数据库中提取的,所以没用datagridview,就像求问大神们 ,应该怎样才能完成双击修改的问题。拜谢了

解决方案 »

  1.   

    labeledit为true,也只能修改第一列的单元格内容,其他列单元格修改不了,所以你最好再做一个form2,双击listview,把当前行传给form2,再form2里用文本框的形式编辑好后点“保存”按钮,把修改后的内容再传回到listview里
      

  2.   

    数据从哪儿来,用DataGridView也可以啊。双击事件创建一个编辑框去修改即可。
      

  3.   

    加个panel。里面有文本框和按钮。平时不显示,双击listview的时候在选中的地方显示出来,并显示选中的内容。点击按钮就保存到listview里。关掉panel
      

  4.   


    //获取鼠标点击的项------API
            [DllImport("user32")]
            public static extern int GetScrollPos(int hwnd, int nBar);        private TextBox txtInput;        //获取点击项的位置
            private void lViewPersonWork_MouseDoubleClick(object sender, MouseEventArgs e)
            {
                try
                {
                    ListViewItem item = this.lViewPersonWork.GetItemAt(e.X, e.Y);                //找到文本框
                    Rectangle rect = item.GetBounds(ItemBoundsPortion.Entire);
                    int StartX = rect.Left; //获取文本框位置的X坐标
                    int ColumnIndex = 0;    //文本框的索引                //获取列的索引
                    //得到滑块的位置
                    int pos = GetScrollPos(this.lViewPersonWork.Handle.ToInt32(), 0);
                    foreach (ColumnHeader Column in lViewPersonWork.Columns)
                    {
                        if (e.X + pos >= StartX + Column.Width)
                        {
                            StartX += Column.Width;
                            ColumnIndex += 1;
                        }
                    }                if (ColumnIndex < this.lViewPersonWork.Columns.Count - 1)
                    {
                        return;
                    }                this.txtInput = new TextBox();                //locate the txtinput and hide it. txtInput为TextBox
                    this.txtInput.Parent = this.lViewPersonWork;                //begin edit
                    if (item != null)
                    {
                        rect.X = StartX;
                        rect.Width = this.lViewPersonWork.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 lViewPersonWork_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)
                {
                    
                }
            }以上就是实现双击ListView单元格在当前单元格上显示一个一样大小的文本框,输入文字之后回车保存并释放掉文本框,以及在不回车的情况下双击其他单元格时释放文本框