18
votes

Everyone probably knows what I mean, but to clarify the control would need to:

  • Fire an event when user edits the text. The event would provide a SuggestionList: TStrings which you could fill with matches/suggestions.
  • if the SuggestionList is not empty a drop down should appear.
  • Unlike combo, the control should not attempt to automatically select/auto complete or otherwise affect the editing.

So, is there a Delphi edit/combo control that works like that ?

5

5 Answers

25
votes

Use the autocompletion feature built in to all Windows edit controls.

First, fill your TStrings object however you want. Then use GetOleStrings to create a TStringsAdapter to wrap it. (The adapter does not claim ownership of the TStrings object, so you must make sure you don't destroy it while the adapter is still live.) The adapter gives you an IStrings interface, which you'll need because the autocompletion feature requires an IEnumString interface to provide the completion matches. Call _NewEnum for that.

Next, call CoCreateInstance to create an IAutoComplete object. Call its Init method to associate it with the window handle of your edit control. If you're using a combo box, then send it a cbem_GetEditControl message to find the underlying edit window.

You can stop at that point and autocompletion should work automatically. You can disable autocompletion if you want, or you can set any number of autocompletion options.

You say you don't want autocompletion, but in the OS terminology, I think what you really don't want is called auto append, where the remainder of the string is entered into the edit box automatically as the user types, but selected so that further typing will overwrite it, and the user needs to delete the excess text if the desired value is shorter than one of the matches.

There is also auto suggest, which displays a drop-down list of suggestions.

You can enable either or both options. You don't need to filter the list of suggestions yourself; the autocomplete object filters the IEnumString list by itself.

4
votes

You can use standard TComboBox and faststrings library (for stringMatches() function).

procedure TForm1.cbChange(Sender: TObject);
var
  s:Integer;
  tmpstr:string;
begin
  //suggestions: tstringlist
  cb.AutoComplete:=false;
  tmpstr:=cb.Text;
  cb.Items.Clear;
  for s:=0 to suggestions.Count - 1 do
    if StringMatches(suggestions[s],cb.Text+'*') then
      cb.Items.Add(suggestions[s]);
  cb.DroppedDown:=(cb.Items.Count<>0) and (Length(cb.Text)<>0);
  cb.Text:=tmpstr;
  cb.SelStart:=Length(cb.Text)
end;
3
votes

If you just want to show a file or url list:

SHAutoComplete(GetWindow(eb_MyComboBox->Handle, GW_CHILD), SHACF_AUTOSUGGEST_FORCE_ON | SHACF_FILESYS_DIRS);
2
votes

I first implemented this feature like Rob described it in his answer. Later I saw that TComboBoxEx has the property AutoCompleteOptions where I set acoAutoSuggest to True and acoAutoAppend to False. The ComboBox now filters its item list when doing some entry and shows the matching items.

I'm using RAD Studio 10 Seattle and XE2 but don't know if this feature is available in older versions.

1
votes

To the last bit of your question: "So, is there a Delphi edit/combo control that works like that ?":

A bit late to the party but yes, I have written a free and open source component that implements the Google Place Autocomplete and Google Place Details API's:

It does inherit from the standard TComboBox but you can modify the code to work with any TEdit

https://carbonsoft.co.za/components/

or

https://github.com/RynoCoetzee/TRCGPlaceAutoCompleteCombo