Posted by webmaster Guido on June 15, 2003 In Reply to: unique values in combobox posted by Stefan Loeners on June 15,
2003 If you want to keep the original order of the items in the Delphi combobox, try the following code: procedure TForm1.btnRemoveDoublesClick(Sender: TObject); var SL: TStringList; i: integer; begin SL := TStringList.Create; with ComboBox1 do begin for i := 0 to Items.Count - 1 do // Add every item to a stringlist, except... // ...if it is already in the list if SL.IndexOf(Items[i]) < 0 then SL.Add(Items[i]); Items.Assign(SL); // replace items with contents of stringlist end; SL.Free; end; This works well up to about 1000 items (depending on the speed of the computer). If you have say 10000 or more items, this method becomes too slow. Then you can use the following method, but it will put the items in alphabetical order: procedure TForm1.btnRemoveDoublesClick(Sender: TObject); var SL: TStringList; begin SL := TStringList.Create; SL.Sorted := True; // must be set, otherwise next line has no effect SL.Duplicates := dupIgnore; // don't accept duplicates SL.AddStrings(ComboBox1.Items); ComboBox1.Items.Assign(SL); SL.Free; end; Search for: ComboBox Stringlist / TStringlist
|
|