listbox删除多余行,代码如下
procedure TForm1.Button2Click(Sender: TObject);
var
i,j,k : integer;
begin
for i := 0 to listbox1.Items.Count - 1 do
for j := 0 to listbox1.Items.Count - 1 do
for k := 0 to listbox1.Items.Count - 1 do
begin
if (listbox1.Items[i] = listbox1.Items[j]) and (i<>j) then //判断是否有相等的
listbox1.Items.Delete(j); //删除重复
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
i,j,k : integer;
begin
for i := 0 to listbox1.Items.Count - 1 do
for j := 0 to listbox1.Items.Count - 1 do
for k := 0 to listbox1.Items.Count - 1 do
begin
if (listbox1.Items[i] = listbox1.Items[j]) and (i<>j) then //判断是否有相等的
listbox1.Items.Delete(j); //删除重复
end;
end;
for i := listbox1.Items.Count - 1 to 0 do
...后面的j,k也是一样的改
for i := ListBox1.Items.Count - 1 downto 0 do
begin
for j := 0 to i - 1 do
begin
if ListBox1.Items[i] = ListBox1.Items[j] then
begin
ListBox1.Items.Delete(i);
Break;
end;
end;
end;记得用downto 不然你删掉一个条目后count就变了,由下至上的拿自己和自己以上的条目比较,如果存在相等的条目,就删除自己。