Delete strings from TStringList

maxfax picture maxfax · Jul 25, 2011 · Viewed 21.2k times · Source

I have a List Box or a List View with items. And I have a String List with the same items (strings) as the List Box/List View. I want to delete all selected items in the List Box/List View from the String List.

How to do?

for i:=0 to ListBox.Count-1 do
  if ListBox.Selected[i] then
    StringList1.Delete(i); // I cannot know exactly an index, other strings move up

Answer

Andreas Rejbrand picture Andreas Rejbrand · Jul 25, 2011
for i := ListBox.Count - 1 downto 0 do
  if ListBox.Selected[i] then
    StringList1.Delete(i);