» Help

How to create a TComboBox with incremental search capabilities

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

Create a new Combo inherited from 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;
Original resource: The Delphi Pool
Author: Unknown
Added: 2009-08-24
Last updated: 2009-08-24

« Return to contents »