Google like edit/combo control for Delphi?

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.

Leave a Comment