使Delphi ListView列项可编辑

1.添加CommCtrl;

2.ListView点击事件

procedure TForm1.ListView1Click(Sender: TObject);
var
  W, X, nCount: Integer;
  Rect: TRect;
  Pos: TPoint;
  nCol: Integer;
  ColSet:set of 0..3;//屏蔽列段
begin
  ColSet:=[0..3];
  if Assigned(ListView1.Selected) then//判断双击的区域是否为有效区域
  begin
      Pos := ListView1.ScreenToClient(Mouse.CursorPos);//
      nCount := ListView1.Columns.Count;
      X := -GetScrollPos(ListView1.Handle, SB_HORZ);

      for nCol := 0 to nCount - 1 do
      begin
        W := ListView_GetColumnWidth(ListView1.Handle, 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 in ColSet) then
      begin
        Exit; //第1,2,3,4列不允许编辑,nCol     就是选定哪一列
      end;

      if X < 0 then
      begin
        exit;
        W := W + X;
        X := 0;
      end;

      Rect := ListView1.Selected.DisplayRect(drBounds);
      Pos.X := X-ListView1.Left;
      Pos.Y := Rect.Top;
      MapWindowPoints(ListView1.Handle, Handle, Pos, 1);

      edtListview.SetBounds(Pos.X, Pos.Y, W, Rect.Bottom- Rect.Top + 3);

      edtListview.Parent := ListView1;
      edtListview.Top := ListView1.Selected.Top;
      edtListview.Text := ListView1.Selected.SubItems[nCurIndex-1];
      edtListview.Visible := True;
      edtListview.SetFocus;
  end;

end;

3.Edit编辑框的改变事件 及 失去焦点事件

procedure TForm1.edtListviewChange(Sender: TObject);
begin
  ListView1.Selected.SubItems[nCurIndex - 1] := edtListview.Text;
end;


procedure TForm1.edtListviewExit(Sender: TObject);
begin
  if edtListview.Text <> '' then
  begin
      ListView1.Selected.SubItems[nCurIndex-1] := edtListview.Text;

  end;
  edtListview.Visible := False;
end;

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值