ListView修改列

本文转载: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;


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值