开发者

Copy all selected items from ListView1 to ListView2

开发者 https://www.devze.com 2023-03-03 11:06 出处:网络
How to copy multiple items from TListView to another. Right now im doing it like this: procedure TForm1.CopyToRightClick(Sender: TObject);

How to copy multiple items from TListView to another. Right now im doing it like this:

procedure TForm1.CopyToRightClick(Sender: TObject);
var
  selected: TListItem;
  addItems: TListItem;
begin
  saveChanges.Visible := false;
  selected := devic开发者_如何学PythoneList.Selected;
  addItems := selectedDevicesList.Items.Add;
  addItems.Assign(selected);
end;

But this way only one selected item get copied. Is there a way to copy all selected items?


You can do

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

to copy every selected list view item in ListView1 to ListView2.

You can do

procedure TForm1.Button2Click(Sender: TObject);
var
  i: Integer;
begin
  ListView1.Items.BeginUpdate;
  try
    ListView2.Items.BeginUpdate;
    try
      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;
    finally
      ListView2.Items.EndUpdate;
    end;
  finally
    ListView1.Items.EndUpdate;
  end;
end;

to move every selected list view item in ListView1 to ListView2.

(This works well in moderately-sized lists. In larger lists, when you need to do something for every selected item, iterating over all items and checking the Selected property is way too slow. Instead, you should use a while loop with GetNextItem with isSelected.)

0

精彩评论

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