E
E
Evgeny Ivanov2019-04-02 13:11:22
Delphi
Evgeny Ivanov, 2019-04-02 13:11:22

How to fix listerror list index out of bounds error?

There is an sListBox1 component on the form (a kind of ListBox1 in Alpha Controls).
Elements are added there In the code, I need to iterate over all the elements of this sListBox1 . Work is in flux.
sListBox1.Items.Add('..........');

procedure TMyThread.Execute;
var
i:integer;
begin
for i:=0 to form1.sListBox1.Items.Count-1 do
begin
full_filename_path:=form1.sListBox1.Items[i];
filename_name:=ExtractFileName(form1.sListBox1.Items[i]);
//.....
end;
end;
end;

When adding more than one file, an error occurs
Error listerror list index out of bounds (1).
How to fix listerror list index out of bounds error?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Tsvetkov, 2019-04-02
@tsklab

The counter bounds are FORcalculated once before the loop. If the main thread changes form1.sListBox1, it may not be the right Items[i]. Or check it out. Or use a loop WHILEthat checks the counter against the condition each time.
Note: The optimizer may change direction FOR.
Tip: don't link modules.
That is, y TMyThreadmust have its own TStrings, into which you need to pass form1.sListBox1.Items.

K
kalapanga, 2019-04-02
@kalapanga

And if in TMyThread.Execute using Synchronize to get the contents of the listbox and then work with this content?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question