Add Incremental Search to ComboBox Items

How can I perform an incremental search in a regular combobox that would work like Delphi's Help/Index functionality?
The best I could do is..

procedure TForm1.cb1KeyPress(Sender: TObject; var Key: Char);
var
  X : integer;
begin
 if CB1.DroppedDown = false then   CB1.DroppedDown := true;
if ((Key > #31) and (Key < #127)) or (Key = #13) then
  begin
   if Key <> #13 then
    CB1.ItemIndex := -1;
   for X := 0 to CB1.Items.Count - 1 do
    if Pos(CB1.Text, CB1.Items[X]) = 1  then Exit;
    end;
end;
beau36Asked:
Who is Participating?
 
intheConnect With a Mentor Commented:
Hi
this works and also catches backspaces
Regards Barry

Code:


procedure TForm1.ComboBox1Change(Sender: TObject);
var
TmpText : string;
i : integer;
begin
with TComboBox(Sender) do begin
TmpText := Text; // save the text that was typed by the user
for i := 0 to Items.Count - 1 do begin
if Pos(Text, Items[i]) = 1 then begin
ItemIndex := i;
SelStart := Length(TmpText);
SelLength := Length(Items[i]) - Length(TmpText);
Break;
end;
end;
end;
end;

procedure TForm1.ComboBox1KeyPress(Sender: TObject; var Key: Char);
begin
if Key = #8 then begin
with TComboBox(Sender) do begin
Text := Copy(Text, 1, Length(Text) - SelLength - 1);
end;
ComboBox1Change(Sender);
Key := #0;
end;
end;
0
 
florisbCommented:

try the OnChange event.

Put there some code that runs through your list and finds and selects it (combobox1.itemIndex := x;)

you shouldn't use pos, it also find substrings. You could write a loop in the loop that compares the CB1.text with the CB1.Items[x]; caracter by caracter, with something like:

//mainloop for.....
begin
....
inputInList := true;
  //caracterloop for.....
  begin
  ...
  if  combobox1.text[y] =combobox1.items[x][y] then
    inputInList := false;
  end;
///if inputInlist is true here, than input is equal to first caracters of item in list, so make it the focused one.
end;

Greetings,
Floris.
0
 
florisbCommented:
I thought there was a standardized way to do this... ..I forgot how.
0
 
VendiCommented:
http://www.borland.com/delphi/news/zd/1998/jul98/

This example uses a list box and edit box to perform an incremental search.
0
All Courses

From novice to tech pro — start learning today.