Programming Listbox Windows API

Description of the Win32 Listbox Control, Styles, Messages

© Guy Lecky-Thompson

How to Create, Manipulate, Search and receive messages from a Windows Win32 AP Listbox Control - language neutral, platform specific.

Introduction

A listbox is a type of control available to Windows programmers that can contain a list of items, usually strings by default, but other types of data can be added in more advanced cases. Typically, a listbox has a right hand side scroll bar, and items are selected by single or double clicking them.

In this how-to, we present the interface to listboxes in a language neutral fashion, referring to common functions that should be available in all Windows programming environments in one form or another. Specifically:

The Windows API also defined some constants for dealing with listboxes which may or may not be available in the compiler toolkit being used:

These equate to values which are typically listed in most Windows API documentation included with the compiler platform. This how-to is Wine compliant, but if a reader finds that some examples do not work with their Linux/Wine implementation, they should comment below, or notify the author.

Creating the Listbox

We create a listbox by calling the CreateWindow API function, giving "listbox" as the window class name in the first parameter. The window name can be an empty string, and the other parameters follow the usual window creation standard for positioning and identifying the parent and control ID. The style parameter can be customized, however, and some common LBS_ values are:

The parent parameter should be set correctly to allow messages to be sent and received from the listbox, and the WM_VISIBLE style must be set for the control to be visible upon creation. If a scroll bar is needed, WS_VSCROLL is required, and if messages are to be received (notification) by the parent window's window procedure, the LBS_NOTIFY style needs to be added.

Adding Items to the Listbox

We add items to the listbox with a call to SendDlgItemMessage, specifying the parent window, control ID, and one of the following:

The LPARAM of the function shall be set to a pointer to the string to be added, while the WPARAM needs to specify the index (zero based) when LB_INSERTSTRING is used, or 0 (zero) otherwise.

Removing Items from the Listbox

We remove items to the listbox with a call to SendDlgItemMessage, specifying the parent window, control ID, and specifying the LB_DELETESTRING message. The LPARAM must be set to 0, and the WPARAM to an existing item index (zero based).

Searching the Listbox

There are two messages that can be used to search the listbox:

These only work with list boxes containing text. The LPARAM of the SendDlgItemMessage function call contains a pointer to the search string, which is never case sensitive. Therefore 'mystring' and 'MyStrinG' are considered to be equivalent. If the listbox does not contain strings, then the parent window receives a WM_COMPAREITEM message in order to allow the application to ascertain whether two items are equal or not.


The copyright of the article Programming Listbox Windows API in Windows Programming is owned by Guy Lecky-Thompson. Permission to republish Programming Listbox Windows API must be granted by the author in writing.




Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo