One thing most programmers who program for windows will have to deal with is the Listbox.
The Listbox is a very useful item in the programmers arsenal. It’s cousin, the ComboBox is a close second. You can use a Listbox to display a list of items to a user and allow them to select and item (or multiple items) and then do something with those items.
I’m not going to get too detailed into the how’s and why’s of Listboxes but will instead show you some very simple re-usable code that I release to you under a Creative Commons Attribution-ShareAlike 2.5 License . All I ask is that you do not claim the code as your own and give me proper credit for it. If you have some good ideas on improving this small code snippet then you can leave a comment below and I will update the article to show your changes.
I first wrote this snippet back in 2002 and it was written while coding for Windows 98 machines.
The first thing every bit of code needs is a header file:
/*—————————————————-
Basic Listbox Code (re-usable)
(c) Stephen De Chellis 2002
Creative Commons Attribution-ShareAlike 2.5 License
Listbox.h - header file for Listbox.cpp
—————————————————-*/
#ifndef LISTBOX_H
#define LISTBOX_H
struct BigBuffer{
char Buffer[256];
};
/* Prototypes for Listbox.c */
extern int FillListBox (HWND hwnd, int ID, struct BigBuffer buffer[], int s);
/* end of Listbox.h */
#endif
Now that we have the header file we can go forward with the cpp file. Before I go ahead, please note the pre-proccesor statements I put in the listbox.h file. They are very important as once your program gets large enough you may inadvertantly include the same header file twice. The if statement, followed by declaring the constant insures that an include file will not be added twice. That’s a headache you want to avoid!
Now let us look at the contents of listbox.cpp
/*—————————————————-
Basic Listbox Code (re-usable)
(c) Stephen De Chellis 2002
Creative Commons Attribution-ShareAlike 2.5 License
Listbox.cpp - Contains code for filling a Listbox
—————————————————-*/
#include <windows.h>
#include “Listbox.h”
int FillListBox (HWND hwnd, int ID, struct BigBuffer buffer[], int s)
{
/********************************************************************
hwnd - handle to window with the listbox
ID - ID number of listbox to be filled with data
buffer[] - String Array that will be written to listbox
s - how many elements of string array to write
This function fills listboxes and then returns an int denoting
the quantity of items listed.
*********************************************************************/
int i=0;
SendDlgItemMessage (hwnd, ID, LB_RESETCONTENT, 0,0);
do
{
SendDlgItemMessage(hwnd, ID, LB_ADDSTRING, i, (LPARAM) buffer[i].Buffer);
i++;
} while(i<s);
return i;
}
I do appologize for some of the formating issues on this blog. I try to get things to look as I like them and then the backend throws in a ton of code that I do not want…
The core of the above function is to take a structure and fill it with a series of strings. You then send the structure and an integer telling the function how many items (in that structure) you want to have sent to the Listbox. The function then returns i, which should be equal to the number of items sent. If it does not return the same number as the number of items sent then you know an error has occured and you can write some code to fix it.
I have since re-written this code for wxWidgets and included much better error handling.
The above code is very simple and you will probably not use it as is unless you are new to programming.
The first line in the FillListBox function is a call to SendDlgItemMessage (hwnd, ID, LB_RESETCONTENT, 0,0). This line simply tells the program to clear all of the contents out of the Listbox (ID) in question. If you do not clear the contents first when the program executes the next block of code you will find yourself adding entries to the entries already there. You do not always clear a listbox before sending items to it, but in this basic example we are clearing it and fully re-populating each time.
The following loop then adds strings to the Listbox, one-by-one, until it has added as many items as it was told to add. when the loop ends this function returns the number of items added.
This is only the first in a series of programming articles. If there are any requests for further help in dealing with Listboxes please comment below.
I used the services of Code Colorizer to colorize the above code snippets.