Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Auto Complete combo control

Posted on 2001-07-17
9
Medium Priority
?
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

721 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