FireMonkey的TComboEdit的自动完成功能

时间:2016-06-03 09:58:54

标签: delphi firemonkey delphi-xe4

VCL.TComboBox有一个属性AutoComplete,可为控件的编辑部分提供自动完成功能。

FMX.TComboEdit是否提供此功能?

1 个答案:

答案 0 :(得分:1)

它不存在,但你可以写自己的。

这是组合框的示例,但您可以更改代码以匹配comboedit

{Combobox default behavior}

TCombobox = class(FMX.ListBox.TComboBox)
  private
    LastTimeKeydown:TDatetime;
    Keys:string;
  protected
    procedure KeyDown(var Key: Word; var KeyChar: System.WideChar; Shift: TShiftState);override;
  end;

{ TCombobox }

procedure TCombobox.KeyDown(var Key: Word; var KeyChar: System.WideChar; Shift: TShiftState);
var
  aStr:string;
  I: Integer;
begin
  if key=vkReturn then exit;
  if (keychar in [chr(48)..chr(57)]) or (keychar in [chr(65)..chr(90)]) or (keychar in [chr(97)..chr(122)]) then begin
    //combination of keys? (500) is personal reference
    if MilliSecondsBetween(LastTimeKeydown,Now)<500 then
      keys:=keys+keychar
    else // start new combination
      keys:=keychar;
    //last time key was pressed
    LastTimeKeydown:=Now;
    //lookup item
    for I := 0 to count-1 do
      if uppercase(copy(items[i],0,keys.length))=uppercase(keys) then begin
        itemindex:=i;
        exit;  //first item found is good
      end;
  end;
  inherited;
end;