zoukankan      html  css  js  c++  java
  • listview 之编辑

    ListView双击产生可编辑输入的TextBox(转)
    2009-09-20 19:34
    运用鼠标事件point 类 Point mousePos = new Point(0,0);
    在ListView的鼠标事件下
            private void listView1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
             {
                
    //record mouse position
                 mousePos.X = e.X;
                 mousePos.Y
    = e.Y;
             }
    然后对ListView的双击事件处理
           //found clicked item
       ListViewItem item = listView1.GetItemAt(mousePos.X,mousePos.Y); //获取鼠标所点击在哪一列上
    存在ListView产生滑块,这样就不能精确获得显示列的X,Y值,为获取滑块滑动的距离,可以使用API来得到
    using System.Runtime.InteropServices;          
    [DllImport("user32")]
    public static extern int GetScrollPos(int hwnd, int nBar) ;

                    //locate text box
                     Rectangle rect = item.GetBounds(ItemBoundsPortion.Entire);
                    
    int StartX = rect.Left;
                    
    int ColumnIndex = 0;
                    
    //get ColumnIndex
                    //得到滑块的位置
                    int pos = GetScrollPos(this.
    listView1.Handle.ToInt32(),0);
                    foreach (ColumnHeader Column in listView1.Columns)
                     {
                        
    if (mousePos.X + pos >=StartX + Column.Width)
                         {                                    
                             StartX
    +=Column.Width;
                             ColumnIndex
    += 1;
                         }

                     }

                    
    //locate the txtinput and hide it. txtInput为TextBox
                     txtInput.Parent = listView1;
                    
    //begin edit
                    if(item != null)
                     {
                         rect.X
    = StartX;
                         rect.Width
    = listView1.Columns[ColumnIndex].Width; //得到长度和ListView的列的长度相同
                         txtInput.Bounds
    = rect;
                        
    //show textbox
                         txtInput.Text = item.SubItems[ColumnIndex].Text;
                         txtInput.Visible
    = true;
                         txtInput.Focus();
                     }
    向ListView加载数据
    ListViewItem item;
    foreach(DataRow dr in ds.Tables[0].Rows)
    {
          item = new ListViewItem(dr[0].ToString());
           item.subItem.add(dr[1].ToString());
         ListView.Item.Add(item);
    }
  • 相关阅读:
    数据库创建索引的缺点,和什么时候不该创建索引
    创建数据库,表,索引,删除索引,查看表中的索引和如何使用表中的索引
    java容器中 哪些是线程安全的
    java中集合类详解
    高并发 问题怎么解决
    数据库20个问题(关于事务、存储引擎、索引、悲观锁乐观锁)
    数据库事务(什么是事务)
    Application对象详解
    get和post 两种基本请求方式的区别
    BZOJ1003物流運輸 DP + SPFA
  • 原文地址:https://www.cnblogs.com/zhangjun1130/p/1669570.html
Copyright © 2011-2022 走看看