ListView修改列
来源:互联网 发布:淘宝买的药是真的吗 编辑:程序博客网 时间:2024/05/16 13:58
本文转载:http://hi.baidu.com/letwin/blog/item/1356262236f90faf4623e89b.html
实现思路:双击时找到当前选择的行,并通过列遍历找到具体的列,找到行列就可以定位单元格了,然后读出来数据到Edit,同时设置Edit显示,当离开Edit时把具体的值写回ListView。
窗体的试验控件如下:
object LV: TListView Left = 8 Top = 8 Width = 297 Height = 289 Columns = < item Caption = '姓名' Width = 70 end item Caption = '工作' Width = 80 end item Caption = '年龄' Width = 55 end item Caption = '刘添加' Width = 80 end> GridLines = True ReadOnly = True RowSelect = True TabOrder = 0 ViewStyle = vsReport OnDblClick = LVDblClick end object Button1: TButton Left = 8 Top = 344 Width = 75 Height = 25 Caption = 'Button1' TabOrder = 1 OnClick = Button1Click end object edtAge: TEdit Left = 8 Top = 312 Width = 121 Height = 21 BevelEdges = [] BevelInner = bvNone BevelOuter = bvNone TabOrder = 2 Visible = False OnChange = edtAgeChange OnExit = edtAgeExit OnKeyPress = edtAgeKeyPress end
Button1代码如下:
var tmpItem: TListItem;begin tmpItem := lv.Items.Add; tmpItem.Caption := 'a'; tmpItem.SubItems.Add('工任地'); tmpItem.SubItems.Add('0'); tmpItem.SubItems.Add('YY');end;
新添加的TEdit事件如下:
procedure TForm1.edtAgeChange(Sender: TObject);begin LV.Selected.SubItems[nCurIndex - 1] := edtAge.Text;end;procedure TForm1.edtAgeExit(Sender: TObject);begin if edtAge.Text <> '' then begin LV.Selected.SubItems[nCurIndex-1] := edtAge.Text; edtAge.Visible := False; end;end;
procedure TForm1.edtAgeKeyPress(Sender: TObject; var Key: Char);begin if Key = #13 then edtAge.OnExit(Sender);//失去焦点end;
主要在下面的双击或SelectItem事件中(本例选择双击事件):
var W, X, nCount: Integer; Rect: TRect; Pos: TPoint; nCol: Integer;begin if Assigned(LV.Selected) then//判断双击的区域是否为有效区域 begin Pos := LV.ScreenToClient(Mouse.CursorPos); nCount := LV.Columns.Count; X := -GetScrollPos(LV.Handle, SB_HORZ); for nCol := 0 to nCount - 1 do begin W := LV.Columns[nCOL].Width;//nCOL是你要修改的那列宽度 if Pos.X <= X + W then begin Break; end; X := X + W end; nCurIndex := nCol; if nCol = nCount then begin Exit; end; if nCol = 0 then begin LV.DeleteSelected; //若添加了此行,则表示点击了第一列会删除此行 Exit; //第1列不允许编辑 end; if LV.Columns[nCol].Caption <> '年龄' then //不是要编辑的那列 begin LV.DeleteSelected; Exit; end; if X < 0 then begin W := W + X; X := 0; end; Rect := LV.Selected.DisplayRect(drBounds); edtAge.SetBounds(X, Rect.Top, W, Rect.Bottom- Rect.Top + 3); edtAge.Parent := LV; edtAge.Top := LV.Selected.Top; edtAge.Text := LV.Selected.SubItems[nCurIndex-1]; edtAge.Visible := True; edtAge.SetFocus; end;end;
最后,别忘了给全局变量做个声明与初始化:
var Form1: TForm1; nCurIndex: integer;//声明procedure TForm1.FormCreate(Sender: TObject);begin nCurIndex := 0;end;
- ListView-修改标题列
- ListView修改列
- ListView动态修改列宽
- 修改列
- ListView里,根把某列的值,修改多行背景色。
- ListView点击列排序
- ListView列排序(c#)
- c#listview隐藏列
- ListView-添加标题列
- ListView列宽自适应
- listview列宽调整
- Mysql修改列,删除列
- SQL 增加列、修改列、删除列
- MySQL 添加列,修改列,删除列
- mysql 添加列,修改列,删除列。
- MySQL 添加列,修改列,删除列
- mysql 添加列,修改列,删除列
- MySQL 添加列,修改列,删除列
- 程序员,2012,不再生活在别处
- 解决Android 工程出现 unable to get system library for the project 异常
- as3 内存释放相关
- 书中自有颜如玉,书中自有黄金屋
- C# 窗体无边框 实现窗体移动(可在指定位置)
- ListView修改列
- ---------------------------谨以此文献给我的2011-----------------------------------
- 使用mpich2配置linux服务器集群
- 手把手教你如何实现Binder的客户端程序(图文)
- 描述程序员牛人:牛人为什么牛?
- WPF中多个StoryBoard动画切换控制
- 天气预报API
- 我脑残实验室Verilog学习笔记——在Xilinx ISE中运用符号设计顶层模块的方法
- 请求一个url 获得返回结果(doPost doGet方式)