Recent

Author Topic: Autocomplete in text field with large amount of items  (Read 1675 times)

fr.luca

  • Newbie
  • Posts: 3
Autocomplete in text field with large amount of items
« on: February 18, 2024, 04:23:56 pm »
Hello,
I'm writing a form for anagraphical data entry, and I would like city of birth & residence fields to autocomplete. The list of items to deal with is quite large, i.e. the 8000 cities of my country (Italy).
Firstly I tried to work it out using a combobox, populated at runtime with the query result from a proper db (see hereunder the code).
In order to make the application as portable as it may, and having to deal with both Windows and Linux clients, I had to notice a very different performance, in terms of time response, between the two environments, on machines with similar capabilities.
On Windows, the population of the item list took about 2 seconds, which is a long but reasonable time for my need; on Linux, the duration of the same operation increases 10 times, forcing OS to send a signal about task not responding.
Actually, it's not necessary for me to use a combobox, which control I choose only because of native autocomplete feature.
Any suggestions? Thank you.

  if openDB('codfisc.db')
  then
  begin
     combo.Clear;
     try
       dbquery.SQL.Text:= 'SELECT DESCR FROM COMUNI ORDER BY DESCR';
       dbquery.Open;
       while not dbquery.EOF do
       begin
         combo.Items.Add(dbquery.FieldByName('DESCR').AsString);
         dbquery.Next;
       end;
     finally
        closeDB;
     end;
  end;


paweld

  • Hero Member
  • *****
  • Posts: 1419
Re: Autocomplete in text field with large amount of items
« Reply #1 on: February 18, 2024, 04:30:38 pm »
Use BeginUpdate - should significantly speed up the addition of data to the ComboBox. eg.
Code: Pascal  [Select][+][-]
  1. if openDB('codfisc.db') then
  2.   begin
  3.     combo.Items.BeginUpdate;
  4.     combo.Clear;
  5.     try
  6.       dbquery.SQL.Text := 'SELECT DESCR FROM COMUNI ORDER BY DESCR';
  7.       dbquery.Open;
  8.       while not dbquery.EOF do
  9.       begin
  10.         combo.Items.Add(dbquery.FieldByName('DESCR').AsString);
  11.         dbquery.Next;
  12.       end;
  13.     finally
  14.       closeDB;
  15.     end;
  16.     combo.Items.EndUpdate;
  17.   end;  
  18.  
Best regards / Pozdrawiam
paweld

fr.luca

  • Newbie
  • Posts: 3
Re: Autocomplete in text field with large amount of items
« Reply #2 on: February 18, 2024, 05:08:51 pm »
Thank you for the hint!
unfortunately, on linux time response remains the same order.
Perhaps  the combobox are not intended to work with similar amount of data, maybe I should have to seek for some external tool...


dseligo

  • Hero Member
  • *****
  • Posts: 1522
Re: Autocomplete in text field with large amount of items
« Reply #3 on: February 18, 2024, 06:24:40 pm »
Thank you for the hint!
unfortunately, on linux time response remains the same order.
Perhaps  the combobox are not intended to work with similar amount of data, maybe I should have to seek for some external tool...

Try to put data in TStringList instead of combobox, just for test. Maybe the problem is in database and not in combobox.

fr.luca

  • Newbie
  • Posts: 3
Re: Autocomplete in text field with large amount of items
« Reply #4 on: February 18, 2024, 10:20:41 pm »
So, I followed your suggestion: a stringlist is now filled up when the form is created, then combo picks it up at showtime.
The form takes some time before appearing, but that's ok.
By the way,  I tried to use a text file instead of  the former sqlite db, to store the list to be retrieved. Nothing significant occurred.
I suppose there should be some kind of tuning in file accessing and management, but I know nothing about the matter.
Anyway, thank you a lot, now it works decently!

 

 

TinyPortal © 2005-2018