Solved

Auto Complete combo control

Posted on 2001-07-17
9
207 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 100 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

929 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now