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[] = 
{ 
        {"Alan", "Center field", 17, 56, .375, "Cannelloni"}, 
        {"Colin", "Pitcher", 26, 96, .456, "Lefse"}, 
        {"Cindy", "Second base", 13, 58, .207, "Tequila"}, 
        {"Dave", "First base", 28, 138, .508, "Suds"}, 
        {"David", "Center field", 18, 101, .612, "Bok Choy"} 
        {"Jack", "Pitcher", 27, 110, .542, "Animal Crackers"}, 
        {"Julie", "Right field", 22, 101, .509, "Mashed Potatoes"}, 
        {"Karen", "Second base", 26, 140, .238, "Pez"}, 
        {"Kathie", "Left field", 26, 127, .353, "Baba Ganouj"}, 
        {"Matt", "Shortstop", 24, 112, .579, "Oats"}, 
        {"Miriam", "Right field", 24, 112, .393, "Zotz"}, 
        {"Pete", "Shortstop", 26, 90, .608, "Beet"}, 
        {"Seth", "Center field", 20, 76, .407, "Otter Pop"}, 
        {"Suzanna", "Catcher", 16, 53, .286, "Toast"}, 
        {"Wendy", "Third base", 25, 154, .493, "Ham"}, 
    }; 
 
BOOL APIENTRY DlgTeamProc( 
    HWND hDlg,            // window handle to 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; 
    } 
}