Solved

Autocomplete Combobox

Posted on 2008-06-19
14
4,548 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
[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
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

726 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