Recent

Author Topic: TComboBox with full text search  (Read 3053 times)

apeoperaio

  • Sr. Member
  • ****
  • Posts: 272
TComboBox with full text search
« on: February 20, 2019, 01:01:57 pm »
Dear All,
I need a TComboBox able to filter the list not only looking at the beginning of the items but on the whole string of every item.
Anyone has already developed something similar?
Any suggestion?
Best regards,
Andrea

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: TComboBox with full text search
« Reply #1 on: February 20, 2019, 06:02:05 pm »
I need a TComboBox able to filter the list not only looking at the beginning of the items but on the whole string of every item.
Like this?
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, StdCtrls;
  9.  
  10. type
  11.   TForm1 = class(TForm)
  12.     ComboBox1: TComboBox;
  13.     procedure ComboBox1Change(Sender: TObject);
  14.     procedure ComboBox1CloseUp(Sender: TObject);
  15.     procedure ComboBox1DropDown(Sender: TObject);
  16.     procedure FormCreate(Sender: TObject);
  17.     procedure FormDestroy(Sender: TObject);
  18.   private
  19.     FFullList: TStringList;
  20.   end;
  21.  
  22. var
  23.   Form1: TForm1;
  24.  
  25. implementation
  26.  
  27. {$R *.lfm}
  28.  
  29. uses StrUtils;
  30.  
  31. procedure TForm1.ComboBox1Change(Sender: TObject);
  32. begin
  33.   ComboBox1.DroppedDown := False;
  34. end;
  35.  
  36. procedure TForm1.ComboBox1CloseUp(Sender: TObject);
  37. begin
  38.   ComboBox1.Items.Clear;
  39. end;
  40.  
  41. procedure TForm1.ComboBox1DropDown(Sender: TObject);
  42. var
  43.   Items: TStrings;
  44.   Line, What: string;
  45. begin
  46.   Items := ComboBox1.Items;
  47.   What := Trim(ComboBox1.Text);
  48.   for Line in FFullList do
  49.     if AnsiContainsText(Line, What) then
  50.       Items.Append(Line);
  51. end;
  52.  
  53. procedure TForm1.FormCreate(Sender: TObject);
  54. begin
  55.   ComboBox1.AutoSelect := False;
  56.   FFullList := TStringList.Create;
  57.   FFullList.Text :=
  58.     'abcdefghij' + LineEnding +
  59.     'fghijklmno' + LineEnding +
  60.     'klmnopqrst' + LineEnding +
  61.     'pqrstuvwxy' + LineEnding +
  62.     'uvwxyz' + LineEnding;
  63. end;
  64.  
  65. procedure TForm1.FormDestroy(Sender: TObject);
  66. begin
  67.   FFullList.Free;
  68. end;
  69.  
  70. end.

apeoperaio

  • Sr. Member
  • ****
  • Posts: 272
Re: TComboBox with full text search
« Reply #2 on: February 21, 2019, 08:59:21 am »
I am looking for something similar to your example.

In order to make your example working I set AutoDropDown to True, but it is not perfect,
if I type 'a' the whole 'abcdefghij' string is written in the combobox and highlighted for a second, then disappear and 'a' remains in the combo.
I found this for Delphi: https://stackoverflow.com/questions/9466547/how-to-make-a-combo-box-with-fulltext-search-autocomplete-support
But I was not able to make it working on Lazarus.
Any hint?

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: TComboBox with full text search
« Reply #3 on: February 21, 2019, 08:18:12 pm »
« Last Edit: February 21, 2019, 08:21:48 pm by Lainz »

Ever

  • Jr. Member
  • **
  • Posts: 61
Re: TComboBox with full text search
« Reply #4 on: February 21, 2019, 09:31:09 pm »
Thanks for the contributions, I'll try to see how it goes, the funny thing is that I do not understand why something that worked very well in version 1.8.4. it got worse in 2.0.0., it has no logic

apeoperaio

  • Sr. Member
  • ****
  • Posts: 272
Re: TComboBox with full text search
« Reply #5 on: February 22, 2019, 03:41:18 pm »

 

TinyPortal © 2005-2018