Search code examples
delphifiremonkeydelphi-10-seattletlistview

Setfocus to the searchbox in a tlistview


I am working in Seattle, writing a FM application for windows only.

I have A tlistview on my form and have it populated with data.

I have the search option turned on.

How do I programmatically set focus to the search box?

How do I increase the size and font size of the search box?

thanks


Solution

  • The searchbox is not intended to be accessed programmatically except for setting it visible and to fire event when changed. Otherwise it is intended to be accessed only by the user. Therefore, access is a little bit involved. However, the example of the OnSearchChange event inspired the following answer:

    uses ..., FMX.SearchBox;
    
    type
      TForm17 = class(TForm)
        ListView1: TListView;
        Button1: TButton;
        Label1: TLabel;
        ...
        procedure Button1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
        sb: TSearchBox; // a local reference
      ...
      end;
    
    implementation
    
    procedure TForm17.Button1Click(Sender: TObject);
    begin
      if Assigned(sb) then
        sb.SetFocus;
    end;
    
    procedure TForm17.FormCreate(Sender: TObject);
    var
      i: integer;
    begin
      ListView1.SearchVisible := True; // or set in the Object Inspector at design time
      for i := 0 to ListView1.Controls.Count-1 do
        if ListView1.Controls[I].ClassType = TSearchBox then
        begin
          sb := TSearchBox(ListView1.Controls[i]);
          Break;
        end;
    end;
    
    procedure TForm17.ListView1SearchChange(Sender: TObject);
    begin
      if Assigned(sb) then
        Label1.Text := sb.Text;
    end;
    

    At form creation we search the SearchBox control and if found we store a reference to it in the sb: TSearchBox; field. Then access is quite straightforward.