I have a listbox and add items to it, items are address of files, items are added after some processes and they are inserted like this:
Listbox_Browser.Items := myItems;
so as im not adding them one by one i cant check them during inserting ti listbox, i tried to check them in OnDraw and used a code like this:
Try
FileOpenandP(Listbox_Browser.Items[Index]);
Except
ListBox_Browser.Items.Delete(Index);
End;
but i got the error "List index out of bounds", what is the solution?
The OnDrawItem
event is for drawing only. You are not supposed to be managing your list inside that event, only drawing its current items as needed.
Instead of assiging the whole list at one time, you should be checking the files first, then assign the remaining list to the ListBox, eg:
I := 0;
while I < myItems.Count do
begin
try
FileOpenandP(myItems[I]);
Inc(I);
except
myItems.Delete(I);
end;
end;
ListBox_Browser.Items := myItems;
If you don't want myItems
altered, use a separate list instead:
tmpItems := TStringList.Create;
try
tmpItems.Assign(myItems);
I := 0;
while I < tmpItems.Count do
begin
try
FileOpenandP(tmpItems[I]);
Inc(I);
except
tmpItems.Delete(I);
end;
end;
ListBox_Browser.Items := tmpItems;
finally
tmpItems.Free;
end;
Or:
ListBox_Browser.Items := myItems;
I := 0;
while I < ListBox_Browser.Items.Count do
begin
try
FileOpenandP(ListBox_Browser.Items[I]);
Inc(I);
except
ListBox_Browser.Items.Delete(I);
end;
end;
Or:
ListBox_Browser.Items.BeginUpdate;
try
ListBox_Browser.Items.Clear;
I := 0;
for I := 0 to myItems.Count-1 do
begin
try
FileOpenandP(myItems[I]);
except
Continue;
end;
ListBox_Browser.Items.Add(myItems[I]);
end;
finally
ListBox_Browser.Items.EndUpdate;
end;