|
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.
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;
}
}
| Last news from Greatis Software |
 |
|
Nostalgia .Net |
|
.Net is powerful, but not all-powerful, so sometimes we need to use Win32 API for our .Net applications. It's simple enough with Platform Invoke if you have Win32 skill, but we do not always have time to dig the ancient documentation, declare the special types that are compatible with Win32, find the values of the Win32's constants and so on. Nostalgia .Net offers several simple-to-use classes, and components that will allow you to forget about the headache of Win32 and just use the power of Win32 in your application the same way as you use the native. Net classes. More » |
| Recommended software for developers |
 |
|
Ultimate Pack |
|
Component pack for Delphi and C++ Builder that contains runtime form designer, runtime object inspector, print suite and much more for the very special price. More » |
 |
|
Form Designer .Net |
|
Unique runtime form design solution that allows to edit any form in .Net WinForms application at runtime with full source codes for only 300 euro! More » |
 |
|
Print Suite .Net |
|
Print Suite .Net is a set of components for easy printing texts, images and grids from your WinForms applications. Full C# source codes are available More » |
 |
|
Gradient Controls .Net |
|
Gradient Controls .Net offers controls with gradient background feature. Labels, panels and so on... Full C# source codes are available More » |
 |
|
Greatis iGrid |
|
iGrid plots drawing grid right over your desktop, so you can use it everywhere, with any drawing application without any special plugins for different graphic editors. More » |
All the contacts and projectsDmitry Vasiliev (just.dmitry)
Related LinksSoftware for Visual Studio .NET developers Software for Delphi and C++ Builder developers Software for Visual Basic 6 developers Delphi Tips&Tricks MegaDetailed.NET More Online Helps Win32 Programmer's Reference Win32 Multimedia Programmer's Reference OLE Programmer's Reference Microsoft Windows Pen API Programmer's Reference Microsoft Windows Sockets 2 Reference Microsoft Windows Telephony API (TAPI) Programmer's Reference Unix Manual Pages
|