Locate Item In ComboBox

Hi,

I have a combobox in which I load 100
items, the items are all parts with a starting code of say 'ABC Motor.'.

If at runtime I type 'A' in the combobox I want the the combobox to go to the nearest of the items just like Table.GoToNearest;.

How can I do this.

Asw
LVL 1
AswAsked:
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.

TheNeilCommented:
Easy - just add the following to the OnKeyUp method of your ComboBox.

VAR
  n : INTEGER;
  New_Text : STRING;
begin
  New_Text := '';
  FOR n := 0 TO (ComboBox1.Items.Count - 1)
  DO
    IF Pos(ComboBox1.Text, ComboBox1.Items[n]) = 1
    THEN
      New_Text := ComboBox1.Items[n];

  IF New_Text <> ''
  THEN
    ComboBox1.Text := New_Text;
end;

The code will search through the items and select the one that matches. If it can't find it then the text in the ComboBox is unaffected

The Neil
0
manjushanCommented:
Hi,
    U r asking for something like a lookupcombo box. There is a TDBLookupcombo. And we have a lot of 3rd party comps for it. Go to Delphi mag or about.delphi.com and look for the com. Or in ur query use Name like A% & fill up the box again.
                                                          All the best
                                                             Manju
0
geobulCommented:
Hi Asw,

Try this piece of code and you will get BACKSPACE working too:

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;

Regards, Geo
0
AswAuthor Commented:
Hi Chaps,

Many thanks for all the comments.

I have to give the points to the procedure I used, so geobul if you could post your comment a an ansewer I shall give you the points.

Many Thanks

Andy
0
geobulCommented:
Hi Asw,

Here you are !

Good luck and
Regards, Geo
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.