Mega Code Archive

 
Categories / Delphi / Examples
 

Easy way to avoid adding duplicate items to string lists

String lists (TStringList) are great -- easy to manage, used by many components such as memos, list boxes, combo boxes, etc., and full of features. Here's how to avoid adding duplicate items to string lists: var sl : TStringList; begin sl := TStringList.Create; with sl do begin Sorted := True; Duplicates := dupIgnore; // // add your items here // all the duplicate items // will be ignored // Add( edit1.text ); end; // // now you can assign the // string list with unique // items to listbox1 (example) // listbox1.Items.Assign( sl ); sl.Free; end;