🙂
First thing, my code
procedure TForm1.Button3Click(Sender: TObject);
var tempId,i:integer;
begin
tempId:=strtoint(edit5.Text);
plik:=TStringList.Create;
plik.LoadFromFile('.\klienci\'+linia_klient[id+1]+'.txt');
if (plik.Count=1) then
begin
label6.Caption:='then';
if (tempId=StrToInt(plik[0])) then
begin
Label6.Caption:='Zwrócono';
plik.Delete(0);
end
end
else
for i:=0 to plik.Count-2 do
begin
if (tempId=StrToInt(plik[i])) then
begin
Label6.Caption:='Zwrócono';
plik.Delete(i);
end;
end;
plik.SaveToFile('.\klienci\'+linia_klient[id+1]+'.txt');
plik.Free;
end;
- When
for i:=0 to plik.Count-2 doI can delete any element but not
last. - When
for i:=0 to plik.Count-1 doI can delete any element without
but from end to start. Because otherwise List index out of bounds.
What’s going one? How can I safety search and remove elements from TStringList?
When deleting intems from list you want to use
downtoloop, ieThis ensures that if you delete item, the loop index stays valid as you move from the end of the list dowards beginning of the list.