首页 > 解决方案 > 带有 ComboBox 的 IAutoComplete 不会触发 OnChange 事件

问题描述

精简版

当组合框中的文本发生更改时,如何触发OnChange事件?

长版

我正在使用IAutoComplete2“自动建议”下拉)列表添加到组合框。

当用户在建议列表中选择不同的选项时,组合框中的文本会发生变化,但遗憾的是没有触发OnChange事件:

AutoComplete附加到裸TEdit时,触发OnChange事件。

function SHAutoCompleteComboShell(AComboBox: TComboBox): IAutoComplete2;
var
    cbi: TComboBoxInfo;
    hEdit: HWND; //A handle to the edit box.
    strings: IEnumString;
    ac: IAutoComplete2;
const
    Default_TComboBoxInfo: TComboBoxInfo = ();
begin
    //Example public domain pseudocode on how to autocomplete a combobox. 
    Result := nil;
    if AComboBox = nil then
        Exit;

    //Get the EDIT control inside the Combobox
    cbi := Default_TComboBoxInfo;
    cbi.cbSize := sizeof(TComboBoxInfo);
    if not GetComboBoxInfo(AComboBox.Handle, {var}cbi) then
        Exit;
    hEdit := cbi.hwndItem; //A handle to the edit box.
    if hEdit = 0 then
        Exit;

    //Suggest strings from the shell namespace
    strings := CreateComObject(CLSID_ACListISF) as IEnumString;

    ac:= CreateComObject(CLSID_IAutoComplete) as IAutoComplete2;
    ac.Init(hEdit, strings, nil, nil);
    ac.SetOptions(ACO_AUTOSUGGEST);

    Result := ac;
end;

即使用户最终Enter选择一个选项,TComboBox 的OnChange仍然不会触发。

我假设 Windows编辑控件正在发送一条EN_CHANGE通知消息,说明其文本已更改;但 TComboBox 并不关心。

当然,TComboBox 很有csDropDown风格(否则我为什么要问这个问题)。

试过Delphi 5Delphi XE6

标签: delphicomboboxautocomplete

解决方案


推荐阅读