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;


 

原创粉丝点击