Главная страница | назад





Article #26614: How do I find a string in a ListBox?

Question

How do I search my listbox for a particular string?

Answer

There are three control messages you can use with list boxes that will allow you to search for strings and/or select them. These messages are:
LB_FINDSTRING
LB_SELECTSTRING
LB_FINDSTRINGEXACT
All three of them are used similarly. Note: they will only search the list box's items for strings beginning with the string you search for, not containing.
Example:

int index = ListBox1->Perform(LB_FINDSTRING,-1,(LPARAM)Edit1->Text.c_str());
if (index != -1)
ListBox1->ItemIndex = index;
or:
int index = ListBox1->Perform(LB_SELECTSTRING,-1,(LPARAM)Edit1->Text.c_str());
//this one will automatically select the string if it is found.

Last Modified: 12-JAN-01