zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    程序员追求的是什么?
    jQuery弹出层始终垂直居中,相对于屏幕,相对于当前窗口
    在VMware上制作一个简单的Linux
    Nginx 反向代理+Varnish 技术
    在做微信分享到朋友圈时,手机扫码报config:invalid signature,分享后后正常的问题,是url问题...
    phpStudy本地环境测试,打开网页很慢的解决办法!
    windows上安装redis并安装php5.6的redis扩展
    Redis 安装
    redis 查看的版本
    who is 规格严格
  • 原文地址:https://www.cnblogs.com/xuezhu/p/14889575.html
Copyright © 2011-2022 走看看