找到你要的答案

Q:Google like edit/combo control for Delphi?

Q:谷歌喜欢编辑/组合控制的德尔福?

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 ?

每个人都可能知道我的意思,但要澄清控制需要:

  • 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.

那么,有一个Delphi编辑/组合控件,这样工作吗?

answer1: 回答1:

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.

使用内置的所有Windows编辑控件的自动完成功能。

首先,填写你的tstrings对象但是你想。然后用getolestrings创造tstringsadapter包了。(适配器不主张所有权的tstrings对象,所以你必须确保你不会破坏它而适配器仍然活着。)给你一个istrings适配器接口,你需要因为自动完成功能需要提供完成ienumstring接口匹配。电话_newenum。

下一步,调用CoCreateInstance创建IAutoComplete对象。调用它的init方法与你的编辑控件的窗口句柄关联。如果你使用一个组合框,然后将它发送一个消息cbem_geteditcontrol探讨编辑窗口。

你可以在这一点上停止并自动完成应自动工作。如果你想的话你可以禁用自动完成,或者你可以设置任意数量的自动完成选项。

你说你不想自动完成的,但是在操作系统的术语,我觉得你真的不想被称为自动添加,在字符串的其余部分进入编辑框自动为用户类型,但选择从而进一步分型将覆盖它,和用户需求,如果期望值比一个火柴删除多余的文字。

也有自动建议,其中显示一个下拉列表的建议。

您可以启用两个或两个选项。你不需要过滤的建议自己的列表;自动完成对象的过滤器列表本身ienumstring。

answer2: 回答2:

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;

TCombobox和faststrings你可以使用标准库(用于stringmatches()功能)。

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;
answer3: 回答3:

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);

如果只想显示文件或URL列表:

SHAutoComplete(GetWindow(eb_MyComboBox->Handle, GW_CHILD), SHACF_AUTOSUGGEST_FORCE_ON | SHACF_FILESYS_DIRS);
answer4: 回答4:

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.

我第一次实现这个功能,就像Rob在他的回答中描述的那样。后来我看到TComboBoxEx在我acoautosuggest真实和虚假的产权autocompleteoptions acoautoappend。现在的项目列表框过滤器做一些条目时显示匹配项。

我使用RAD Studio XE2 10西雅图,但不知道这个功能在老版本是可用的。

delphi  autocomplete  combobox