views:

288

answers:

3

What is the correct way to implement the "find as you type" behavior on a TComboBox descendant component whose style is csOwnerDrawFixed?

A: 

You could override TCustomComboBox.KeyPress (in StdCtrls) and duplicate the csDropDown behaviour.

Bruce McGee
A: 

Depends, do you only want to implement the "search as you type" part, without displaying any feedback to the user (the way Firefox does it on long lists, for example), or do you want to show an small sub-control displaying the current search string?

dguaraglia
+1  A: 
  1. Use a TTimer (let's call it timIncSearch). Set (at design time) the following properties: Enabled:=False;
    Interval:=400; //empirically found - it's the delay used in Windows Explorer

...and in OnTimer you'll wrote your searching engine. Be sure that the first line here will be timIncSearch.Enabled:=False; Also because you use csOwnerDrawFixed perhaps it's better to enforce a repaint of your control.

As an aside, - just guessing because you didn't give us many details - perhaps you must hook OnEnter and OnExit events to custom open and close the DropDown list. (Normaly, this is achieved by setting the AutoDropDown property accordingly)

  1. In your ComboBox.KeyPress you'll write

with timIncSearch do
begin
Enabled:=False;
Enabled:=True;
end;

...also take care here, perhaps you must have a 'case Key of' construct to handle the #13 separately (or whatever).

Other hints:

  • depending on your situation, perhaps you must hook (also?) the OnKeyDown (if you want to process special keys like eg. BackSpace, Del, Arrows etc. - taking in account that the event repeats itself while the key it's pressed down) and/or OnKeyUp (if you want to do similar processing as above but without taking in account the keyboard's key auto-repeat feature).