Table of Contents

Items and Autocomplete

TTMSFNCComboBox displays selectable items in a drop-down list. It can also work as an editable picker when users should type a value or narrow the list through autocomplete.

Combo box drop-down list

Populate Items

Use Items or AddItem to build the list. Set ItemIndex when the combo box needs an initial selection.

var
  I: Integer;
begin
  TMSFNCComboBox1.Items.Clear;

  for I := 1 to 10 do
    TMSFNCComboBox1.AddItem('Item ' + I.ToString);

  TMSFNCComboBox1.ItemIndex := 0;
  TMSFNCComboBox1.OnItemSelected := TMSFNCComboBox1ItemSelected;
end;

Enable Editing and Autocomplete

Set Style to csDropDown when users may type in the editor. Enable AutoComplete, AutoDropDown, and AutoCompleteNumChar to open and filter the list after a short typed prefix.

Editable combo box with text entry
TMSFNCComboBox1.Style := csDropDown;
TMSFNCComboBox1.AutoComplete := True;
TMSFNCComboBox1.AutoDropDown := True;
TMSFNCComboBox1.AutoCompleteDelay := 250;
TMSFNCComboBox1.AutoCompleteNumChar := 2;
TMSFNCComboBox1.DropDownCount := 8;
TMSFNCComboBox1.CaseSensitive := False;

Handle Selection

Handle OnItemSelected to receive both the selected text and item index. This is the safest place to synchronize dependent controls or store the selected value.

procedure TForm1.TMSFNCComboBox1ItemSelected(Sender: TObject; AText: string; AItemIndex: Integer);
begin
  Caption := 'Selected ' + AText + ' at index ' + AItemIndex.ToString;
end;

Combining item population, autocomplete, and selection handling

This is the full setup for a country-picker combobox: populate from a list, enable typed autocomplete after two characters, and react to selection to drive a dependent control:

procedure TForm1.FormCreate(Sender: TObject);
var
  Country: string;
begin
  // Populate items
  TMSFNCComboBox1.Items.Clear;
  for Country in GetCountryList do
    TMSFNCComboBox1.AddItem(Country);
  TMSFNCComboBox1.ItemIndex := 0;

  // Autocomplete: editable style + open after 2 chars
  TMSFNCComboBox1.Style               := csDropDown;
  TMSFNCComboBox1.AutoComplete        := True;
  TMSFNCComboBox1.AutoDropDown        := True;
  TMSFNCComboBox1.AutoCompleteNumChar := 2;
  TMSFNCComboBox1.CaseSensitive       := False;
  TMSFNCComboBox1.DropDownCount       := 8;

  // Selection handler
  TMSFNCComboBox1.OnItemSelected := ComboBoxItemSelected;
end;

procedure TForm1.ComboBoxItemSelected(Sender: TObject;
  AText: string; AItemIndex: Integer);
begin
  // Update a dependent control with the selected country
  PhoneCodeEdit.Text := PhoneCodeFor(AText);
end;

See Also