?
Solved

Autocomplete Combobox

Posted on 2008-06-19
14
Medium Priority
?
4,580 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 200 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 200 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 38

Assisted Solution

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

or try the Jedi VCL
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 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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

762 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