将所有选定的项目从 ListView1 复制到 ListView2

发布于 2024-11-05 13:01:29 字数 360 浏览 1 评论 0原文

如何将多个项目从 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;

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

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 := deviceList.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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

错々过的事 2024-11-12 13:01:29

您可以将

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;

ListView1 中的每个选定列表视图项目复制ListView2

您可以将

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;

ListView1 中的每个选定列表视图项目移动ListView2

(这在中等大小的列表中效果很好。在较大的列表中,当您需要对每个选定的项目执行某些操作时,迭代所有项目并检查 Selected 属性太慢了。相反,您应该将 while 循环与 GetNextItemisSelected 结合使用。)

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文