delphisearchfiremonkey

Delphi FMX SearchBox


I have a project in Delphi FMX for Android and Windows platform on which I need to have a search box. The search box must have a search string input option, which when a character is entered automatically filters the data and displays only those matching the search string in a drop down list. Clicking on a selected entry in the drop-down list shall cause the search field to record the selected string.

I approached the problem by adding a search box consisting of a TSearchBox in a TComboBox

(see Figure 1).

The TComboBox is binded to the TQuery with the data. I filter the data in the TQuery according to the string input in the TSearchBox and display the drop-down menu of the TComboBox. The problem is that the list in TComboBox is refreshed only if I shrink and expand the drop-down menu again, and that when I click on a selected item in the list, the ComboBox1Click procedure is not executed. My code is like this:

TfrmMain.SearchBox1Typing(Sender: TObject);
begin
  ComboBox1.BeginUpdate;
  dmCC.FDQuery1.Filtered := False;
  dmCC.FDQuery1.Filter := '';
  if SearchBox1.Text <> '' then
  begin
    dmCC.FDQuery1.OnFilterRecord := nil;
    dmCC.FDQuery1.Filter := 'UPPER(name) LIKE ''/' + UpperCase(SearchBox1.Text) + '%'' ESCAPE ''/''';
    dmCC.FDQuery1.Filtered := True;
  end;
  ComboBox1.EndUpdate;
  // shrink and expand the drop-down menu to refresh data
  ComboBox1.DropDown;
  if NOT ComboBox1.DroppedDown then
  begin
    ComboBox1.DropDown;
  end;
end;

procedure TfrmMain.ComboBox1Click(Sender: TObject);
begin
  SearchBox1.Text := ComboBox1.Text;
end;

Does anyone have any idea how to implement such a search box?


Solution

  • I wrote this post in my blog 5 years ago. I also wrote a tutorial Sorry, all in French. Hope it can help