该OnDrawItem
活动仅用于绘画。您不应该在该事件中管理您的列表,只根据需要绘制其当前项目。
您应该先检查文件,然后将剩余的列表分配给 ListBox,而不是一次分配整个列表,例如:
I := 0;
while I < myItems.Count do
begin
try
FileOpenandP(myItems[I]);
Inc(I);
except
myItems.Delete(I);
end;
end;
ListBox_Browser.Items := myItems;
如果您不想myItems
更改,请改用单独的列表:
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;
或者:
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;
或者:
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;