• 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);
    }
  • 相关阅读:
    【译】System.Text.Json 的下一步是什么
    渲染路径(Rendering Path):Forward、Deferred、LightPrePass、Tiled、Clustered
    前端面试汇总
    微前端建设方案
    微前端说明以及使用
    大家好,新来到贵宝地,想跟大家一起学习,一起交流,请大伙多多指教。
    Active Directroy user and computers 批量查询被Disable的user 和长时间没有登录的User
    Powershell 启动windows server
    iTerm2与oh my zsh的安装使用记录
    Linux 命令行编辑快捷键[转]
  • 原文地址:https://www.cnblogs.com/zhangjun1130/p/1669570.html
Copyright © 2020-2023  润新知