How do I find a string in a ListBox?

By: mykle hoban

Abstract: This article describes how to use the WinAPI to find and select strings in a ListBox control

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.


Server Response from: ETNASC03