views:

725

answers:

2

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 ?

+4  A: 

You can use standart 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;
barism
Yes, this is close, but has few issues. When you enter first character in an empty combo, for a split second it will autocomplete it before you set the cb.text back to what it was.Also if you move the focus to some other control while the dropdown is visible it will again autocomplete. And if you use the up/down keys to navigate the drop down items the text will again get autocompleted. And lastly you set the SelStart to the last character, but it should be set to where it was, so SelStart needs to be stored at the begging of the method and restored at the end.
Daniel Maurić
+4  A: 

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.

Rob Kennedy
This is nothing short of perfect, precisely what I want, I wasn't aware of autocompletion options, thanks.
Daniel Maurić