The listbox specific flag LBS_NOSEL used in the window style parameter simply means that the user cannot select items in the listbox. Alternates to this would be not to specify LBS_NOSEL permitting one single line to be selected (this is the default action), LBS_MULTISEL permitting multiple selections and LBS_EXTENDEDSEL which permits multiple selections using the keyboard keys. The WS_VSCROLL in combination with the LBS_DISABLENOSCROLL says that if there are fewer items in the listbox than can be displayed, do not hide the scroll window.

To add items to a listbox, we use the SendMessage function with a message type LB_ADDSTRING. This will add a NULL teminated string to the end of the list.

SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"Martyn.Rae");

Finding a string in the listbox

As the list is now sorted into order, the last item in the list is not "Martyn.Rae". So, to find the index position of that string in the list, we use the SendMessage function with a message type LB_FINDSTRING and the string we want to find (this function is case-insensitive - to find an exact string match use the LB_FINDSTRINGEXACT function). We can then select that index item in the list using LB_SETCURSEL.

A listbox cannot be used if there are going to be more than 32,767 items in the listbox. The mechanism used by the listbox for allocating memory for large numbers of items is very inefficient, but you can use the SendMessage function with a message type LB_INITSTORAGE to allocate space to contain the items.

Example Code

The code example below demonstrates the use of a simple listbox that contains name strings. The list is sorted and will find and select the listbox item for the string "Martyn.Rae".