Reputation: 3
I've wanted to create such simple multiple selection allowed listbox and to do that, I've done the following: BTW I'm using visual studio 2013 ultimate
#include <iostream>
#include "Windows.h"
#include "Strsafe.h"
#include "Afxres.h"
typedef struct
{
TCHAR achName[MAX_PATH];
TCHAR achPosition[12];
int nGamesPlayed;
int nGoalsScored;
} Player;
Player Roster[] =
{
{TEXT("Haas, Jonathan"), TEXT("Midfield"), 18, 4 },
{TEXT("Pai, Jyothi"), TEXT("Forward"), 36, 12 },
{TEXT("Hanif, Kerim"), TEXT("Back"), 26, 0 },
{TEXT("Anderberg, Michael"), TEXT("Back"), 24, 2 },
{TEXT("Jelitto, Jacek"), TEXT("Midfield"), 26, 3 },
{TEXT("Raposo, Rui"), TEXT("Back"), 24, 3},
{TEXT("Joseph, Brad"), TEXT("Forward"), 13, 3 },
{TEXT("Bouchard, Thomas"), TEXT("Forward"), 28, 5 },
{TEXT("Salmre, Ivo "), TEXT("Midfield"), 27, 7 },
{TEXT("Camp, David"), TEXT("Midfield"), 22, 3 },
{TEXT("Kohl, Franz"), TEXT("Goalkeeper"), 17, 0 },
};
INT_PTR CALLBACK ListBoxExampleProc(HWND hDlg, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
{
// Add items to list.
HWND hwndList = GetDlgItem(hDlg, IDC_LISTBOX_EXAMPLE);
for (int i = 0; i < ARRAYSIZE(Roster); i++)
{
int pos = (int)SendMessage(hwndList, LB_ADDSTRING, 0,
(LPARAM) Roster[i].achName);
// Set the array index of the player as item data.
// This enables us to retrieve the item from the array
// even after the items are sorted by the list box.
SendMessage(hwndList, LB_SETITEMDATA, pos, (LPARAM) i);
}
// Set input focus to the list box.
SetFocus(hwndList);
return TRUE;
}
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDOK:
case IDCANCEL:
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
case IDC_LISTBOX_EXAMPLE:
{
switch (HIWORD(wParam))
{
case LBN_SELCHANGE:
{
HWND hwndList = GetDlgItem(hDlg, IDC_LISTBOX_EXAMPLE);
// Get selected index.
int lbItem = (int)SendMessage(hwndList, LB_GETCURSEL, 0, 0);
// Get item data.
int i = (int)SendMessage(hwndList, LB_GETITEMDATA, lbItem, 0);
// Do something with the data from Roster[i]
TCHAR buff[MAX_PATH];
StringCbPrintf (buff, ARRAYSIZE(buff),
TEXT("Position: %s\nGames played: %d\nGoals: %d"),
Roster[i].achPosition, Roster[i].nGamesPlayed,
Roster[i].nGoalsScored);
SetDlgItemText(hDlg, IDC_STATISTICS, buff);
return TRUE;
}
}
}
return TRUE;
}
}
return FALSE;
}
Altough I've included everything I need I get the following errors:
Error:identifer "IDC_LISTBOX_EXAMPLE" is undefined.
Why I'm getting such an error ? Is there anyone to help me ?
EDIT
I'm not using MFC template , do I have to ?
Upvotes: 0
Views: 3202
Reputation: 1297
Ok, so, you have nothing, except dialog procedure. Ok.
File -> New... -> Project:
Then Ok -> Next. You need to create dialog based application. For this, create empty application:
and click Finish.
Now you have empty project. You need to add dialog resource.
Right click on project -> Add -> Resource. And then Dialog -> New:
You will have empty dialog. Right click on them -> Properties. Remember the value of ID (IDD_DIALOG1 in my case):
Then you need to add list box control. Go to ToolBox window (View -> Toolbox, Ctrl+Alt+X), select ListBox and drag&drop to dialog:
OK. Then you need to know the ID of your list box (the same as for dialog: Right click on list box control -> Properties -> ID). It's IDC_LIST2 in my case.
For your example you need to add in the same way Static Text Control (In the same way as with list box: Toolbox -> Static Text -> drag& drop to dialog form). Give another ID for this static control (Right click -> Properties -> Id -> Change to IDC_STATISTICS):
THATS ALL YOU NEED TO DO WITH YOUR DIALOG :)
And now - the code. Add new cpp file to the project:
#include <Windows.h>
#include <tchar.h>
#include <strsafe.h>
#include "resource.h"
INT_PTR CALLBACK ListBoxExampleProc(HWND hDlg, UINT message,
WPARAM wParam, LPARAM lParam);
int CALLBACK _tWinMain(
_In_ HINSTANCE hInstance,
_In_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ int nCmdShow)
{
::DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, ListBoxExampleProc);
return 0;
}
typedef struct
{
TCHAR achName[MAX_PATH];
TCHAR achPosition[12];
int nGamesPlayed;
int nGoalsScored;
} Player;
Player Roster[] =
{
{TEXT("Haas, Jonathan"), TEXT("Midfield"), 18, 4 },
{TEXT("Pai, Jyothi"), TEXT("Forward"), 36, 12 },
{TEXT("Hanif, Kerim"), TEXT("Back"), 26, 0 },
{TEXT("Anderberg, Michael"), TEXT("Back"), 24, 2 },
{TEXT("Jelitto, Jacek"), TEXT("Midfield"), 26, 3 },
{TEXT("Raposo, Rui"), TEXT("Back"), 24, 3},
{TEXT("Joseph, Brad"), TEXT("Forward"), 13, 3 },
{TEXT("Bouchard, Thomas"), TEXT("Forward"), 28, 5 },
{TEXT("Salmre, Ivo "), TEXT("Midfield"), 27, 7 },
{TEXT("Camp, David"), TEXT("Midfield"), 22, 3 },
{TEXT("Kohl, Franz"), TEXT("Goalkeeper"), 17, 0 },
};
INT_PTR CALLBACK ListBoxExampleProc(HWND hDlg, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
{
// Add items to list.
HWND hwndList = GetDlgItem(hDlg, IDC_LIST2);
for (int i = 0; i < ARRAYSIZE(Roster); i++)
{
int pos = (int)SendMessage(hwndList, LB_ADDSTRING, 0,
(LPARAM) Roster[i].achName);
// Set the array index of the player as item data.
// This enables us to retrieve the item from the array
// even after the items are sorted by the list box.
SendMessage(hwndList, LB_SETITEMDATA, pos, (LPARAM) i);
}
// Set input focus to the list box.
SetFocus(hwndList);
return TRUE;
}
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDOK:
case IDCANCEL:
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
case IDC_LIST2:
{
switch (HIWORD(wParam))
{
case LBN_SELCHANGE:
{
HWND hwndList = GetDlgItem(hDlg, IDC_LIST2);
// Get selected index.
int lbItem = (int)SendMessage(hwndList, LB_GETCURSEL, 0, 0);
// Get item data.
int i = (int)SendMessage(hwndList, LB_GETITEMDATA, lbItem, 0);
// Do something with the data from Roster[i]
TCHAR buff[MAX_PATH];
StringCbPrintf (buff, ARRAYSIZE(buff),
TEXT("Position: %s\nGames played: %d\nGoals: %d"),
Roster[i].achPosition, Roster[i].nGamesPlayed,
Roster[i].nGoalsScored);
SetDlgItemText(hDlg, IDC_STATISTICS, buff);
return TRUE;
}
}
}
return TRUE;
}
}
return FALSE;
}
Compile and run! And remember - never use WInAPI (or MFC, no matter) for writing UI. Use Qt!
And the scrren of your app:
Upvotes: 5