TComboBox: How can I remove duplicates from list? lazarus / 31-LCL ---------------------------------------------------------------------- AMJF · 2021-07-18 18:55 I have written a routine that scans the files in a folder and adds them to the TComboBox control on my form, but while I can sort the items alphabetically, I haven't found the means of removing duplicate entries added to the control. Is there a means of doing this, or will I have to resort to a TStringList and then copy it over to the TComboBox to achieve what I want? ---------------------------------------------------------------------- h-elsner · 2021-07-18 19:06 I don't let them in. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure Merkliste(ml: TComboBox; maxNumberItems: integer);begin                                            {fill a DropDownList}  if (ml.Text<>'') and     (ml.Items.IndexOf(ml.Text)<0) then            {not in list yet}    ml.Items.Insert(0, ml.Text);  if ml.Items.Count>MaxNumberItems then               {limit the number of entries}    ml.Items.Delete(MaxNumberItems);end;  However, TComboBox is TStrings and has all on-board to delete, insert and so on. No need for a StringList. br HE ---------------------------------------------------------------------- AMJF · 2021-07-18 19:32 I clean forgot about IndexOf to see if the string was already there or not before addition. Have made the changes, program works fine now. Thanks! ---------------------------------------------------------------------- krexon · 2021-07-20 08:06 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  SL: TStringList;begin  ComboBox1.Items.BeginUpdate;  try    SL := TStringList.Create;    try      SL.Sorted := True; // Required for Duplicates to work      SL.Duplicates := dupIgnore;      SL.AddStrings(ComboBox1.Items);      ComboBox1.Items.Assign(SL);    finally      SL.Free;    end;  finally    ComboBox1.Items.EndUpdate;  end;end; ---------------------------------------------------------------------- JuhaManninen · 2021-07-20 11:31 This is slightly off-topic, not related to only ComboBoxes, but I would like to advertise the unit LookupStringList in LazUtils package. Class TLookupStringList is an unsorted StringList with a fast lookup feature. Internally it uses a balanced tree container (TStringMap) to store the strings again which is then used for Contains, IndexOf and Find methods. It is very memory efficient, because: 1. Strings have reference count and lazy copy semantics. It means only a reference to a string is copied to the other container. 2. A balanced tree is memory efficient compared to a hash map. This class is useful when you must preserve the order in list, but also need to do fast lookups to see if a string exists, or you must prevent duplicates. There is also a function Deduplicate(). ---------------------------------------------------------------------- AlexTP · 2021-07-20 15:06 Juha, Thanks for that info! Useful. Why don't you give link to https://wiki.lazarus.freepascal.org/LazUtils#TLookupStringList ? ---------------------------------------------------------------------- JuhaManninen · 2021-07-20 15:15 Quote from: Alextp on July 20, 2021, 05:06:30 pmWhy don't you give link to https://wiki.lazarus.freepascal.org/LazUtils#TLookupStringList ? I forgot that wiki page. It's information is rather limited. The page is about the whole LazUtils but it lists only 2 items.