Creating a Simple List Box

The following example demonstrates how a dialog box procedure creates a simple list box and fills it with the names of people on a softball team. When a name in the list is selected, additional information about the player is displayed in the dialog box. The following illustration shows the dialog box.

pics/WIN3200000040.gif

The list box has the LBS_STANDARD style, a combination of LBS_SORT, LBS_NOTIFY, WS_VSCROLL, and WS_BORDER. The code initializes the dialog box while processing the WM_INITDIALOG message. For each name that appears in the list box, the code sends an LB_ADDSTRING message to the list box. By processing the LBN_SELCHANGE notification message, the code also keeps track of when the selection changes.

#define BUFFER MAX_PATH

#define NAMELENGTH 15

#define POSITIONLENGTH 20

#define TEAMSIZE 15

typedef struct {

TCHAR tchName[NAMELENGTH];

TCHAR tchPosition[POSITIONLENGTH];

int nGamesPlayed;

int nInningsPlayed;

double xBattingAverage;

TCHAR tchFoodName[NAMELENGTH];

} Player;

Player Roster[] = {

{"Pete", "shortstop", 26, 90, .608, "Rutabaga"},

{"Suzanna", "catcher", 16, 53, .286, "Toast"},

{"Jack", "pitcher", 27, 110, .542, "Animal Crackers"},

{"Karen", "second base", 26, 140, .238, "Pez"},

{"Dave", "first base", 28, 138, .508, "Suds"},

{"Wendy", "third base", 25, 154, .493, "Ham"},

{"Matt", "shortstop", 24, 112, .579, "Oats"},

{"Jenny", "right field", 22, 101, .509, "Mashed Potatoes"},

{"Seth", "left-center field", 20, 76, .407, "Otter Pop"},

{"Kathie", "left field", 26, 127, .353, "Baba Ganouj"},

{"Colin", "pitcher", 26, 96, .456, "Lefse"},

{"Penny", "right field", 24, 112, .393, "Zotz"},

{"Art", "left-center field", 17, 56, .375, "Cannelloni"},

{"Cindy", "second base", 13, 58, .207, "Tequila"},

{"David", "center field", 18, 101, .612, "Bok Choy"}

};

/*

* FUNCTION: DlgTeamProc(HWND, unsigned, UINT, LONG)

*

* PURPOSE: Dialog box for "BFG Softball Statistics"

*/

BOOL APIENTRY DlgTeamProc(

HWND hDlg, /* window handle of dialog box */

UINT message, /* type of message */

UINT wParam, /* message-specific information */

LONG lParam) /* message-specific information */

{

TCHAR tchBuffer[BUFFER];

int nItem;

int i;

HWND hwndList;

switch (message) {

case WM_INITDIALOG:

{

hwndList = GetDlgItem(hDlg, IDL_SOFTBALL);

/* Initialize the list box (fill it with player names). */

for (i = 0; i < TEAMSIZE; i++) {

SendMessage(hwndList, LB_ADDSTRING, 0,

(LPARAM) Roster[i].tchName);

SendMessage(hwndList, LB_SETITEMDATA, i, (LPARAM) i);

}

SetFocus(hwndList);

return FALSE;

}

case WM_COMMAND:

switch (LOWORD(wParam)) {

case IDL_SOFTBALL:

switch (HIWORD(wParam)) {

case LBN_SELCHANGE:

/* Show the selected player's statistics. */

hwndList = GetDlgItem(hDlg, IDL_SOFTBALL);

nItem = SendMessage(hwndList, LB_GETCURSEL,

0, 0);

i = SendMessage(hwndList, LB_GETITEMDATA,

nItem, 0);

SetDlgItemText(hDlg, IDS_POS,

Roster[i].tchPosition);

SetDlgItemText(hDlg, IDS_GAME,

_itoa(Roster[i].nGamesPlayed,

tchBuffer, 10));

SetDlgItemText(hDlg, IDS_INN,

_itoa(Roster[i].nInningsPlayed,

tchBuffer, 10));

SetDlgItemText(hDlg, IDS_BA,

_gcvt(Roster[i].xBattingAverage,

3, tchBuffer));

SetDlgItemText(hDlg, IDS_FOOD,

Roster[i].tchFoodName);

return TRUE;

}

break;

case IDOK:

case IDCANCEL:

/* Destroy the dialog box. */

EndDialog(hDlg, TRUE);

return TRUE;

default:

return FALSE;

}

default:

return FALSE;

}

}

Software for developers
Delphi Components
.Net Components
Software for Android Developers
More information resources
MegaDetailed.Net
Unix Manual Pages
Delphi Examples
Databases for Amazon shops developers
Amazon Categories Database
Browse Nodes Database