Solved

Auto Complete combo control

Posted on 2001-07-17
9
209 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

777 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