Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Auto Complete combo control

Posted on 2001-07-17
9
Medium Priority
?
216 Views
Last Modified: 2010-04-06
Does anyone know of any auto complete combo boxes, where the information is automatically entered based on entries in the list items?
0
Comment
Question by:JazC
9 Comments
 
LVL 17

Expert Comment

by:geobul
ID: 6292402
Hi,
You can do it using the TComboBox. Just add the next two event handlers:

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
      if Pos(Text, Items[i]) = 1 then
      begin
        ItemIndex := i;
        SelStart := Length(TmpText);
        SelLength := Length(Items[i]) - Length(TmpText);
        Break;
      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
 
LVL 17

Expert Comment

by:geobul
ID: 6292417
I forgot to say: this solution is case sensitive with backspace support.
0
 
LVL 22

Accepted Solution

by:
Mohammed Nasman earned 400 total points
ID: 6292433
Hello

  add combo box on the form, and set it's property sorted to true


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    ComboBox1: TComboBox;
    procedure ComboBox1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure ComboBox1Change(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  lastKey: Word;
implementation

{$R *.DFM}

procedure TForm1.ComboBox1Change(Sender: TObject);
var
  SearchStr: string;
  retVal: integer;
begin
  // copy search pattern
  SearchStr := (Sender as TCombobox).Text;

  if lastKey <> VK_BACK then
  begin
    retVal := (Sender as TCombobox).Perform(CB_FINDSTRING, -1, LongInt(PChar(SearchStr)));

    if retVal > CB_Err then
    begin
      (Sender as TCombobox).ItemIndex := retVal;
      (Sender as TCombobox).SelStart := Length(SearchStr);
      (Sender as TCombobox).SelLength :=
        (Length((Sender as TCombobox).Text) - Length(SearchStr));
    end; // retVal > CB_Err

  end; // lastKey <> VK_BACK
  lastKey := 0;
end;


procedure TForm1.ComboBox1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  lastKey := Key;
end;

end.

Best regards
Mohamemd Nasman
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 27

Expert Comment

by:kretzschmar
ID: 6292442
seems i'm too late :-)

from my paq:

Function ReturnIndex(Fragment : String; Strings : TStrings) : Integer;
var I : Integer;
begin
 I := 0;
 while (I < Strings.Count) and
       (pos(Fragment,Strings[I]) <> 1) do inc(i);
 if I = Strings.Count then I := -1;
 result := i;
end;

procedure TForm1.ComboBox1KeyPress(Sender: TObject; var Key: Char);
var
 I : Integer;
 J : Integer;
 S : String;
begin
 if not (Key in [#8,#13]) then
 begin
   J := ComboBox1.SelStart;
   S := copy(ComboBox1.Text,1,J)+key;
   I := ReturnIndex(S,ComboBox1.Items);
   if I > -1 then
   begin
     ComboBox1.Text := ComboBox1.Items[i];
     ComboBox1.SelStart := J+1;
     ComboBox1.SelLength := length(ComboBox1.Text)-J;
   end
   else
   begin
     ComboBox1.Text := s;
     ComboBox1.SelStart := J+1;
     ComboBox1.SelLength := 0;
   end;
   Key := #0;
 end;
end;

maybe it helps you

meikl ;-)
0
 
LVL 3

Expert Comment

by:Stefaan
ID: 6292462
Hi,

Did you check the RxLib combo box controls ? As far as I know they have such a functionality.  At least the lookup comboboxes have, but if I remember it correctly the others have that too.  So why reinvent the wheel ? Just use it again ;-)

Best regards,


Stefaan
0
 
LVL 2

Expert Comment

by:egono
ID: 6293013
listening ...
0
 
LVL 4

Expert Comment

by:jsweby
ID: 6293223
I can send you a Freeware component called TComboBox98 that also does all this for you.

J.
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6293562
if you are looking for component, you will find some free components in torry site
http://www.torry.net/enhancedlistandcomboboxes.htm

Mohammed
0
 

Author Comment

by:JazC
ID: 6296324
I did try some others, but i wanted a nice way to chage my LMDComboBox and add this functionality, so this answer was the easiest method.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question