Tuesday, March 1, 2011

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;

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Kang Iwan K-sev | Thank's for your visit To My Site - Ridwan Mulyana | Cibeureum