Search code examples
delphicomboboxdelphi-7vclautofill

How to fill ComboBox while you are typing in it?


I am trying to accomplish a search as you type in a TComboBox and add items automatically as I type.

I use Delphi 7 and MSSQL.

Lets say I have a long table with name lists in a table with one column named 'names' and I typed 'Jonathan'.

I want to get results into the TComboBox as I type one by one.

Thanks.


Solution

  • Try the following:

    procedure TForm1.ComboBox1Change(Sender: TObject);
    var
      I: Integer;
    begin
      ComboBox1.Items.Clear;
      ComboBox1.SelStart:= Length(ComboBox1.Text); //To put the cursor in the end
                                                     of the string typed in the ComboBox
      if ComboBox1.Text = '' then
        ADOTable1.Filtered:= False
          else
            begin
              ADOTable1.Filter:= 'Names LIKE ' + QuotedStr(ComboBox1.Text + '*');
              ADOTable1.Filtered:= True;
              for I := 1 to ADOTable1.RecordCount do
                begin
                  ADOTable1.RecNo:= I;
                  ComboBox1.Items.Add(ADOTable1.FieldByName('Names').Value);
                end;
            end;
    end;