for I := 0 to listbox1.items.count - 1 do
begin if Pos('记事本',Listbox1.Items[I]) >0 then
begin
ListBox1.Items.Delete(1);
End;
End;删除LISTBOX1中包含'记事本'的行,为什么出错呢?
begin if Pos('记事本',Listbox1.Items[I]) >0 then
begin
ListBox1.Items.Delete(1);
End;
End;删除LISTBOX1中包含'记事本'的行,为什么出错呢?
for I := listbox1.items.count - 1 downto 1 do
begin
if Pos('记事本',Listbox1.Items[I]) >0 then
begin
ListBox1.Items.Delete(i);
End; End;
for i := 0 to listbox1.items.count - 1 do
begin
if Pos('记事本',Listbox1.Items[i]) >0 then
begin
ListBox1.Items.Delete(1);
end;
end;
代码存在两个问题:
1. 循环应该使用downto形式, 否则某个Items[i]删除之后, 原来的Items[i+1]将变为Items[i], 这导致有些Items被漏判。而且, i循环还是要一直到原来的Count-1为止, 这将导致访问Items[i]时下标越界。2. 不是Delete(1), 而是Delete(i)。所以:for i := listbox1.items.count - 1 downto 0 do
begin
if Pos('记事本',Listbox1.Items[i]) >0 then
begin
ListBox1.Items.Delete(i);
end;
end;
都会出现问题的
应该通过循环取出符合条件index放到另一个listbox或数组中,然后再直接delete
参考代码
for I := 0 to listbox1.items.count - 1 do
begin if Pos('记事本',Listbox1.Items[I]) >0 then
begin
ListBox2.Items.Delete(1);
End;
for I := 0 to listbox2.items.count - 1 dobegin
s:=listbox2.items[i];
listbox1.items.delete(strtoint(s));
end;
End;
begin if Pos('记事本',Listbox1.Items[I]) >0 then
begin
ListBox1.Items.Delete(i);
End; End;