Messages to List Boxes

A dialog box procedure can send messages to a list box to add, delete, examine, and change list box items. For example, a dialog box procedure could send an LB_ADDSTRING message to a list box to add an item, and an LB_GETSEL message to determine whether the item is selected. Other messages set and retrieve information about the size, appearance, and behavior of the list box. For example, the LB_SETHORIZONTALEXTENT message sets the scrollable width of a list box. A dialog box procedure can send any message to a list box by using the SendMessage or SendDlgItemMessage function.

A list box item is often referenced by its index, an integer that represents the item's position in the list box. The index of the first item in a list box is 0, the index of the second item is 1, and so on.

The following table describes how the predefined list box procedure responds to list box messages.

Message Response
LB_ADDFILE Inserts a file into a directory list box filled by the DlgDirList function and retrieves the list box index of the inserted item.
LB_ADDSTRING Adds a string to a list box and returns its index.
LB_DELETESTRING Removes a string from a list box and returns the number of strings remaining in the list.
LB_DIR Adds a list of filenames to a list box and returns the index of the last filename added.
LB_FINDSTRING Returns the index of the first string in the list box that matches a given prefix.
LB_FINDSTRINGEXACT Returns the index of the string that is equivalent to or prefixed by a given prefix.
LB_GETANCHORINDEX Returns the index of the item that the mouse last selected.
LB_GETCARETINDEX Returns the index of the item that has the focus rectangle.
LB_GETCOUNT Returns the number of items in the list box.
LB_GETCURSEL Returns the index of the currently selected item.
LB_GETHORIZONTALEXTENT Returns the scrollable width, in pixels, of a list box.
LB_GETITEMDATA Returns the 32-bit value associated with the given item.
LB_GETITEMHEIGHT Returns the height, in pixels, of an item in a list box.
LB_GETITEMRECT Retrieves the client coordinates of the given list box item.
LB_GETLOCALE Retrieves the locale of the list box. The high-order word contains the country code and the low-order word contains the language identifier.
LB_GETSEL Returns the selection state of a list box item.
LB_GETSELCOUNT Returns the number of selected items in a multiple-selection list box.
LB_GETSELITEMS Creates an array of the indexes of all selected items in a multiple-selection list box and returns the total number of selected items.
LB_GETTEXT Retrieves the string associated with a given item and the length of the string.
LB_GETTEXTLEN Returns the length, in characters, of the string associated with a given item.
LB_GETTOPINDEX Returns the index of the first visible item in a list box.
LB_INITSTORAGE Allocates memory for the specified number of items and their associated strings.
LB_INSERTSTRING Inserts a string at a given index in a list box.
LB_ITEMFROMPOINT Retrieves the zero-based index of the item nearest the specified point in a list box.
LB_RESETCONTENT Removes all items from a list box.
LB_SELECTSTRING Selects the first string it finds that matches a given prefix.
LB_SELITEMRANGE Selects a given range of items in a list box.
LB_SELITEMRANGEEX Selects a given range of items if the index of the first item in the range is less than the index of the last item in the range. Cancels the selection in the range if the index of the first item is greater than the last.
LB_SETANCHORINDEX Sets the item that the mouse last selected to a given item.
LB_SETCARETINDEX Sets the focus rectangle to a given list box item.
LB_SETCOLUMNWIDTH Sets the width, in pixels, of all columns in a list box.
LB_SETCOUNT Sets the number of items in a list box.
LB_SETCURSEL Selects a given list box item.
LB_SETHORIZONTALEXTENT Sets the scrollable width, in pixels, of a list box.
LB_SETITEMDATA Associates a 32-bit value with a list box item.
LB_SETITEMHEIGHT Sets the height, in pixels, of an item or items in a list box.
LB_SETLOCALE Sets the locale of a list box and returns the previous locale identifier.
LB_SETSEL Selects an item in a multiple-selection list box.
LB_SETTABSTOPS Sets the tab stops to those specified in a given array.
LB_SETTOPINDEX Scrolls the list box so the specified item is at the top of the visible range.