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

倖福魔咒の 提交于 2020-01-03 04:46:15

问题


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?

Thanks in Advance


回答1:


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;


来源:https://stackoverflow.com/questions/6786625/how-to-copy-multiple-selected-rows-of-listview-into-clipboard-in-delphi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!