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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
intheCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.