Recent

Author Topic: TComboBox: How can I remove duplicates from list?  (Read 3657 times)

AMJF

  • New Member
  • *
  • Posts: 48
TComboBox: How can I remove duplicates from list?
« on: July 18, 2021, 08:55:52 pm »
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

  • Newbie
  • Posts: 6
Re: TComboBox: How can I remove duplicates from list?
« Reply #1 on: July 18, 2021, 09:06:14 pm »
I don't let them in.
Code: Pascal  [Select][+][-]
  1. procedure Merkliste(ml: TComboBox; maxNumberItems: integer);
  2. begin                                              {fill a DropDownList}
  3.   if (ml.Text<>'') and
  4.      (ml.Items.IndexOf(ml.Text)<0) then            {not in list yet}
  5.     ml.Items.Insert(0, ml.Text);
  6.   if ml.Items.Count>MaxNumberItems then               {limit the number of entries}
  7.     ml.Items.Delete(MaxNumberItems);
  8. end;
  9.  

However, TComboBox is TStrings and has all on-board to delete, insert and so on. No need for a StringList.

br HE


AMJF

  • New Member
  • *
  • Posts: 48
Re: TComboBox: How can I remove duplicates from list?
« Reply #2 on: July 18, 2021, 09:32:44 pm »
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

  • Jr. Member
  • **
  • Posts: 80
Re: TComboBox: How can I remove duplicates from list?
« Reply #3 on: July 20, 2021, 10:06:37 am »
Code: Pascal  [Select][+][-]
  1. var
  2.   SL: TStringList;
  3. begin
  4.   ComboBox1.Items.BeginUpdate;
  5.   try
  6.     SL := TStringList.Create;
  7.     try
  8.       SL.Sorted := True; // Required for Duplicates to work
  9.       SL.Duplicates := dupIgnore;
  10.       SL.AddStrings(ComboBox1.Items);
  11.       ComboBox1.Items.Assign(SL);
  12.     finally
  13.       SL.Free;
  14.     end;
  15.   finally
  16.     ComboBox1.Items.EndUpdate;
  17.   end;
  18. end;

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4468
  • I like bugs.
Re: TComboBox: How can I remove duplicates from list?
« Reply #4 on: July 20, 2021, 01:31:33 pm »
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().
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

AlexTP

  • Hero Member
  • *****
  • Posts: 2402
    • UVviewsoft
Re: TComboBox: How can I remove duplicates from list?
« Reply #5 on: July 20, 2021, 05:06:30 pm »
Juha,
Thanks for that info!
Useful.
Why don't you give link to https://wiki.lazarus.freepascal.org/LazUtils#TLookupStringList ?

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4468
  • I like bugs.
Re: TComboBox: How can I remove duplicates from list?
« Reply #6 on: July 20, 2021, 05:15:34 pm »
Why 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.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

 

TinyPortal © 2005-2018