Solved

Autocomplete Combobox

Posted on 2008-06-19
14
4,473 Views
Last Modified: 2012-06-22
Hi.
I'm looking for the following behaviour from the combobox:
User enters partial text, and the combobox drops down (immediately OnChange) with all the suggestions that have a PARTIAL INNER match of the entered text (I.e. *Combobox.Text*).

I looked everywhere and all I could find was the implementation that Auto-Suggests matches at the beginning.


I tried the following code, and it basically works but it messes up the OnEdit event, and the pop-up size of the combo box.

Suggestions for fixes, or ready components are highly appreciated.

unit Unit1;
 

interface
 

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls, ComCtrls;
 

type

  TForm1 = class(TForm)

    ComboBox1: TComboBox;

    procedure FormClose(Sender: TObject; var Action: TCloseAction);

    procedure FormCreate(Sender: TObject);

    procedure ComboBox1Change(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

    ComboStrings:TStrings;
 

  end;
 

var

  Form1: TForm1;
 

implementation
 

{$R *.dfm}
 

procedure TForm1.ComboBox1Change(Sender: TObject);

var

  I: Integer;
 

begin

ComboBox1.Items.Clear;

for I := 0 to ComboStrings.Count - 1 do

  if Pos(ComboBox1.Text,ComboStrings[i])<>0 then

    ComboBox1.Items.Add(ComboStrings[i]);

ComboBox1.DroppedDown:=True;

end;
 

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);

begin

ComboStrings.Free;

end;
 

procedure TForm1.FormCreate(Sender: TObject);

begin

ComboStrings:=TStringList.Create;

ComboStrings.AddStrings(ComboBox1.Items);

end;
 

end.

Open in new window

0
Comment
Question by:duke_n
14 Comments
 
LVL 14

Assisted Solution

by:SteveBay
SteveBay earned 50 total points
ID: 21823755
Raize Components has an MRUComboBox that does that
www.Raize.com
Raize comps are not free but they are so good that I have saved the purchase price hundreds of time over since I started using them.
0
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 50 total points
ID: 21828048
here's a modified version of your code that does what you want except for a slight flicker

  const

    wm_changecombolistitems = WM_USER+101;

...

    procedure Message_UpdateComboBoxListItems(var Msg: TMessage); message wm_changecombolistitems;

...
 

procedure TForm1.ComboBox1Change(Sender: TObject);

begin

  PostMessage(self.handle, wm_changecombolistitems, 0, 0);

end;
 

procedure TForm1.Message_UpdateComboBoxListItems(var Msg: TMessage);

var

  i: integer;

  selstart_, sellength_: integer;

  text_: string;

begin

  selstart_ := ComboBox1.SelStart;

  sellength_ := ComboBox1.SelLength;

  text_ := ComboBox1.Text;
 

  ComboBox1.Items.Clear;

  for i := 0 to ComboStrings.Count - 1 do

  begin

    if (text_ = '') or (Pos(text_, ComboStrings[i]) <> 0) then

      ComboBox1.Items.Add(ComboStrings[i]);

  end;
 

  i := ComboBox1.Items.Count;

  if i > 10 then i := 10;

  ComboBox1.DroppedDown:=False;

  ComboBox1.DropDownCount := i;

  ComboBox1.DroppedDown:=True;

  ComboBox1.Text := text_;

  ComboBox1.SelStart := selstart_;

  ComboBox1.SelLength := sellength_;

end;

Open in new window

0
 
LVL 37

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 50 total points
ID: 21828818
http://www.devexpress.com
have a neat set of editors too, also not free.

or try the Jedi VCL
0
 
LVL 1

Author Comment

by:duke_n
ID: 21839657
TheRealLoki:
I don't see any flicker, however the mouse cursor disappears for some reason, making it very inconvenient to select something out of the combo box.

SteveBay, Geert_Gruwez:
Unfortunately I can't make the project dependent on closed-source third party components. I will check out Jedi VCL though, thanks for the suggestion
0
 
LVL 1

Author Comment

by:duke_n
ID: 21840010
OK I found the fix for disappearing mouse: I moved the PostMessage from the OnChange to the OnKeyUp event.


However, there's another problem I didn't notice before:
if the text I typed is actually a beginning of some of the lines, the combobox forces autocompletion (although I set AutoComplete=False) and I can't type the next letters
0
 
LVL 1

Author Comment

by:duke_n
ID: 21840133
the cursor still disappears in Delphi 7 :(
In delphi 2007 it's alright.
if you know a workaround for the D7 problem, please tell me
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 1

Author Comment

by:duke_n
ID: 21971248
Guys, are you still on this thread ?
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 21971515
oops, sorry, I am super busy atm, but i will try it out in Delphi 7 tomorrow if I get a chance. fingers crossed
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 21979888
it seems fine to me in Delphi 7.
Can you explain a little more about what exactly is happening?
Do you mean you do not wish to show the rest of the "word" in the edit portion?
0
 
LVL 1

Author Comment

by:duke_n
ID: 21991270
yes, because even though I turn off the autocomplete, it for some reason completes the word, and then selects it in the dropped down list.
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 21991382
ok, try changing my code from
ComboBox1.SelLength := sellength_;
to
ComboBox1.SelLength := 0;

I'll check it out tomorrow when I get to my pc

0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 22003685
I can't get it to work completely sorry.
when I get it to not auto complete, it fails to work with ESC, or cursor keys, and vice-versa.
Perhaps there is a better component to achieve this
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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

920 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