我想知道如何比较列表框之间的项目。在我的主窗体上,有两个列表框。我想通过单击事件将项目从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/