将所有选定项目从ListView复制到ListView 2

时间:2011-05-07 22:36:16

标签: delphi listview

如何将多个项目从TListView复制到另一个。现在我这样做:

procedure TForm1.CopyToRightClick(Sender: TObject);
var
  selected: TListItem;
  addItems: TListItem;
begin
  saveChanges.Visible := false;
  selected := deviceList.Selected;
  addItems := selectedDevicesList.Items.Add;
  addItems.Assign(selected);
end;

但这样只会复制一个选定的项目。有没有办法复制所有选定的项目?

1 个答案:

答案 0 :(得分:8)

你可以做到

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  ListView2.Items.BeginUpdate;
  for i := 0 to ListView1.Items.Count - 1 do
    if ListView1.Items[i].Selected then
      ListView2.Items.Add.Assign(ListView1.Items[i]);
  ListView2.Items.EndUpdate;
end;

将<{1}}中的每个选定列表视图项复制ListView1

你可以做到

ListView2

将<{1>}中的每个选定列表视图项移动procedure TForm1.Button2Click(Sender: TObject); var i: Integer; begin ListView1.Items.BeginUpdate; ListView2.Items.BeginUpdate; for i := ListView1.Items.Count - 1 downto 0 do if ListView1.Items[i].Selected then begin ListView2.Items.Add.Assign(ListView1.Items[i]); ListView1.Items[i].Delete; end; ListView2.Items.EndUpdate; ListView1.Items.EndUpdate; end;