Lookup value on input

How can I lookup a value in a table or query while the user is typing in . The cursor (and display) should be closer to the exact value as the user puts in more characters.

I did something like this in delphi 1. It might not be the best solution but it works.

I Keep a TTable open for the value to be searched for. The index must, of course, be on the field that's used in the edit box. In the editbox's change event I use the editbox's value in a FindNearest call on the TTable. Whatever I get as a result is placed back in the editbox's text.

This is only the general way of it. In fact I enable a 1/3 second timer in the change event and do the search (and disable the timer) in the timer's timer event. This allows the user to type rapidely without having to do the search for every character typed.

You may also want to do some special processing for the backspace key or force the selection to the added part of the string.

Instead of returning the results to the edit box (which would overwrite the end-user's entry) display the results in something else - eg a listbox. You could display several near matches this way eg:


procedure Edit1OnChange( ...);

var i:integer;

begin

if not updating then exit; {set updating elsewhere - eg a timer}

updating:= false;

Table1.FindNearest([Edit1.text]);

ListBox1.clear;

i:= 0;

while (i < 5) and (not (table1.eof)) do

  begin

  listbox.items.add(Table1.fields[0].asString);

  inc(i);

  table1.next;

  end;

listbox1.itemindex:= 0;

end;