2008. május 6., kedd

How to create a TComboBox with incremental search capabilities


Problem/Question/Abstract:

Is there a way to let a TComboBox do incremental search in its drop down list? Currently it uses only the first letter so if multiple items start with the same letter it's not very useful.

Answer:

Create a new Combo inherited from class(TcustomComboBox) and change the Change Event to the following few lines:

{...}
protected

procedure Change; override;
{...}

  procedure TExtComboBox.Change;
  var
    str: string;
    Index: Integer;
  begin
    inherited Change;
    str := Text;
    if (FLastKey = VK_DELETE) or (FLastKey = VK_BACK) then
    begin
      SelStart := Length(str);
      SelLength := 0;
      Exit;
    end;
    {try to find the closest matching item}
    Index := Perform(CB_FINDSTRING, -1, LPARAM(str));
    if Index <> CB_ERR then
    begin
      ItemIndex := Index;
      SelStart := Length(str);
      SelLength := Length(Items[Index]) - SelStart;
    end
    else
      Text := str;
    {call standard event}
    if Assigned(FOnChange) then
      FOnChange(Self);
  end;
end.

Nincsenek megjegyzések:

Megjegyzés küldése