开发者

How to copy multiple selected rows of listview into clipboard in Delphi

开发者 https://www.devze.com 2023-03-21 23:50 出处:网络
I have put together this code Selected rows into clipboard from a lisview. procedure TFmainViewTCP.Copy1Click(Sender: TObject);

I have put together this code Selected rows into clipboard from a lisview.

procedure TFmainViewTCP.Copy1Click(Sender: TObject);
 var
  Str:String;
  k  :Integer;
  lItem:TListItem;
 begin
   repeat
     lItem:=lvConnection.Selected;
     Str:=lItem.Caption;
     for k:=0 to lvConnection.Columns.Count-2 do
      begin
       Str:=Str+'  '+lItem.SubItems[k];
      end;
     Clipboard.AsText:=Clipboard.AsText+ sLineBreak +Str; {copy into clipboard}
   until lItem.Selected=True;
 end;

I am not sure if this is working correctly, it does not copy out all the rows for me. Can somebody help me out in this?

Th开发者_开发技巧anks in Advance


your code does not iterate over all of the selected rows. It just works on the first selected. You need to loop on all the items and process the selected ones...

procedure TFmainViewTCP.Copy1Click(Sender: TObject);
 var
  s, t: String;
  i: Integer;
  lItem: TListItem;
 begin
  t := '';
  lItem := lvConnection.GetNextItem(nil, sdBelow, [isSelected]);
  while lItem <> nil do
  begin
    s := lItem.Caption;
    for i := 0 to lItem.SubItems.Count-1 do
      s := s + '  ' + lItem.SubItems[i];
    t := t + s + sLineBreak;
    lItem := lvConnection.GetNextItem(lItem, sdBelow, [isSelected]);
  end;
  if t <> '' then Clipboard.AsText := t;
 end;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号