[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4642
  • Last Modified:

Autocomplete Combobox

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
duke_n
Asked:
duke_n
3 Solutions
 
SteveBayCommented:
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
 
TheRealLokiSenior DeveloperCommented:
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
 
Geert GruwezOracle dbaCommented:
http://www.devexpress.com
have a neat set of editors too, also not free.

or try the Jedi VCL
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
duke_nAuthor Commented:
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
 
duke_nAuthor Commented:
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
 
duke_nAuthor Commented:
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
 
duke_nAuthor Commented:
Guys, are you still on this thread ?
0
 
TheRealLokiSenior DeveloperCommented:
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
 
TheRealLokiSenior DeveloperCommented:
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
 
duke_nAuthor Commented:
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
 
TheRealLokiSenior DeveloperCommented:
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
 
TheRealLokiSenior DeveloperCommented:
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

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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now