Google
 

Saturday, November 17, 2007

Incremental Search for the TListBox Delphi Control



The TListBox Delphi control displays a collection of items in a scrollable list. Items can be selected by clicking on an item, the ItemIndex property gets or sets the index of the selected item. Incremental Searching for a ListBoxImagine a list box with a huge number of (unsorted) items. Finding the one user wants to select might turn into a nightmare. Let's provide the user with an option to immediately locate the item in the list box by adding incremental search functionality. Drop a TEdit and a TListBox on a form. Leave the default names: "Edit1" and "ListBox1".


Handle the Edit1's OnChange event as:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

procedure TListBoxSearchForm.Edit1Change(Sender: TObject) ;

const indexStart = -1;

var search : array[0..128] of Char;

begin

___//make sureLength(Edit1.Text) <= 128

___StrPCopy(search, Edit1.Text) ;

___ListBox1.ItemIndex := ListBox1.Perform(LB_SELECTSTRING, indexStart, LongInt(@search)) ;

end;


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


The StrPCopy
RTL function copies Edit1.Test string value into a null-terminated string variable "search".
The Perform method sends the specific LB_SELECTSTRING message directly to ListBox1.

No comments: