delphi - Delphi项目在2个列表框之间进行比较

标签 delphi delphi-5

我想知道如何比较列表框之间的项目。在我的主窗体上,有两个列表框。我想通过单击事件将项目从1st添加到2nd,但是当使用它时,同一项目将在2nd listbox上相乘。解决“文件已存在”的想法吗?

procedure TForm1.Button1Click(Sender: TObject);
var
  i: integer;
begin
  for i := ListBox1.Items.Count - 1 downto 0 do
    if ListBox1.Selected[i] then                       
      ListBox2.Items.Add(ListBox1.Items.Strings[i]);
end;

最佳答案

如果您有一个单选列表框1:

if Listbox2.Items.IndexOf(Listbox1.Items[Listbox1.ItemIndex]) = -1 then
begin
  // Doesn't exist yet. Safe to add
end;


对于多选(您的代码似乎暗示了这一点):

for i := 0 to ListBox1.Items.Count - 1 do
  if (ListBox1.Selected[i] and (ListBox2.Items.IndexOf(ListBox1.Items[i]) = -1) then                       
    ListBox2.Items.Add(ListBox1.Items[i]);


我认为,后者也适用于单选。

关于delphi - Delphi项目在2个列表框之间进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13110479/

相关文章:

delphi - 在不使用 TDatabase 绕过的情况下禁用登录提示

delphi - DCC32编译器配置文件和命令行参数优先级

Delphi - 未找到 TListView 类

delphi - 如何在不捕获控件焦点的情况下滚动 Firemonkey TListBox?

security - Winhttp - 如果对等证书无效,则阻止成功握手

html - 自动电子邮件发送系统 - 垃圾邮件权重

Delphi换行符

string - Delphi 5中的OleVariant和String分配

Delphi 5 表单标题字体大小

delphi - 枚举类型和助手的可见性