Storing Data in Hidden List Boxes

Using Child Windows as Temporary Data Storage Gets Added Benefits

© Guy Lecky-Thompson

We look at how using list boxes in Windows Win32 GUI programming can provide a simple solution to application data storage using automatic API features.

Introduction

In this article we look at a couple of interesting ways to use the features provided by the Win32 API relating to list boxes. The reader should understand the Windows programming paradigm on the one hand, and some basic programming skills on the other. Although the examples are provided in C code, they ought to be fairly easily portable to other languages.

Creating the Hidden List Box

The list box is created with a simple call to CreateWindow, making sure that the style does not include WS_VISIBLE. The location and size parameters can also all be set to zero. Both of these together will ensure that the list box is never revealed to the user. The handle to it should be retained, or a static child ID used.

Searching and Sorting

List boxes can be created with the style LBS_SORT, which will sort the items, after a fashion. It is not as strict as an alphabetical sort, but works well with most string based data. When items are added with the LB_ADDSTRING message, the list box will automatically insert at the right position and return the index from the SendDlgItemMessage function call.

Searching the list box is also a useful feature of the Windows API. The LB_FINDSTRING and LB_FINDSTRINGEXACT messages work in a similar way:

The difference between the 2 messages is that LB_FINDSTRING will return the index of the first occurrence of a string that starts with the text provided, while LB_FINDSTRINGEXACT works on an exact match only. To get the next occurrence, one needs only to supply the index returned by the first call. If LB_ERR was returned, the string was not found.

Caution: The start index is the index of the item before the item to start searching at. Therefore, to start at the first item, a value of -1 must be used.

An example of this message in action might be:

int nIndex = -1;
do
{
nIndex = SendDlgItemMessage( hParent, nID, LB_FINDSTRING, nIndex, (LPARAM) szText );
if (nIndex != LB_ERR)
{
// Do something
}
} while (nIndex != LB_ERR);

The LB_FINDSTRING and LB_FINDSTRINGEXACT messages do not search in a wrap around way - when the last item has been searched, LB_ERR is returned.

Practical Applications

One practical application of this technique occurs when processing files where the user should be presented with a list of file names (as the result of a search, for example), but where the original full file name with path needs to be retained. We then create 2 list boxes:

We link the 2 together by stripping the path from the file name, inserting it into the Filename list box, and keeping the index to use as the index parameter into the call to insert the full path file name into the hidden list box. If the user selects an item from the Filename list box, we can then extract the correct item from the hidden list box.

When doing this, the programmer has to be sure to use:

As a final note, hidden list boxes can also be used, with the LB_DIR message, to get a directory listing, rather than using the FindFirstFile and FindNextFile API functions. All of these LB_ messages are sent with the SendDlgItemMessage function.


The copyright of the article Storing Data in Hidden List Boxes in Windows Programming is owned by Guy Lecky-Thompson. Permission to republish Storing Data in Hidden List Boxes 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