Solved

Auto Complete combo control

Posted on 2001-07-17
9
204 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
Comment Utility
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
Comment Utility
I forgot to say: this solution is case sensitive with backspace support.
0
 
LVL 22

Accepted Solution

by:
mnasman earned 100 total points
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Expert Comment

by:Stefaan
Comment Utility
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
Comment Utility
listening ...
0
 
LVL 4

Expert Comment

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

J.
0
 
LVL 22

Expert Comment

by:mnasman
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

12 Experts available now in Live!

Get 1:1 Help Now