«« Site Rescued »»

While the site is still going to move from its current host, a new site will now take its place. More Info.

The new site may have less content, but the core will now remain. And it will now play nicely with phones! Keep an eye on the DelphiDabbler Blog for news.

» 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 »