Solved

Autocomplete Combobox

Posted on 2008-06-19
14
4,454 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 36

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

760 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

18 Experts available now in Live!

Get 1:1 Help Now