-
ListBox Question
This is the code I have for my Dialog Box:
Code:
INT_PTR CALLBACK DlgMng(HWND hWndDlg, UINT Message, WPARAM wParam, LPARAM lParam) {
switch(Message) {
case WM_INITDIALOG: {
TCITEM tci;
tci.mask = TCIF_TEXT;
tci.pszText = "Tab 1";
tci.iImage = -1;
TabCtrl_InsertItem(GetDlgItem(hWndDlg,ID_TAB), 0, &tci);
tci.pszText = "Tab 2";
TabCtrl_InsertItem(GetDlgItem(hWndDlg,ID_TAB), 1, &tci);
pane2=true;
NMHDR nmh;
nmh.hwndFrom = hWndDlg;
nmh.idFrom = ID_TAB;
nmh.code = TCN_SELCHANGE;
TabCtrl_SetCurSel(GetDlgItem(hWndDlg,ID_TAB),0);
SendMessage(hWndDlg,WM_NOTIFY,nmh.idFrom,(LPARAM)&nmh);
return true;
}
case WM_NOTIFY:
if(((LPNMHDR)lParam)->code==TCN_SELCHANGE) {
if(pane2) {
SetWindowPos(GetDlgItem(hWndDlg,ID_LIST),NULL,0,0,0,0,SWP_NOZORDER);
SetWindowPos(GetDlgItem(hWndDlg,IDOK),NULL,122,331,45,26,SWP_NOZORDER);
SetWindowPos(GetDlgItem(hWndDlg,IDCANCEL),NULL,182,331,45,26,SWP_NOZORDER);
} else {
SendDlgItemMessage(hWndDlg,ID_LIST,LB_RESETCONTENT,0,0);
SetWindowPos(GetDlgItem(hWndDlg,ID_LIST),NULL,15,37,319,300,SWP_NOZORDER);
SetWindowPos(GetDlgItem(hWndDlg,IDOK),NULL,122,331,45,26,SWP_NOZORDER);
SetWindowPos(GetDlgItem(hWndDlg,IDCANCEL),NULL,182,331,45,26,SWP_NOZORDER);
if(mysql_query(database,"SELECT `id`,`text` FROM `table`")) {
my_error(hWndDlg,true,140,0,mysql_error(database));
return false;
}
result = mysql_store_result(database);
for(int a=0;a<mysql_num_rows(result);a++) {
row = mysql_fetch_row(result);
SendMessage(GetDlgItem(hWndDlg,ID_LIST),LB_ADDSTRING,0,(LPARAM)row[1]);
SendMessage(GetDlgItem(hWndDlg,ID_LIST),LB_SETITEMDATA,a,(LPARAM)row[0]);
}
}
pane2=!pane2;
}
return true;
case WM_COMMAND:
switch(LOWORD(wParam)) {
case IDCANCEL:
EndDialog(hWndDlg,0);
break;
case IDOK:
MessageBox(0,editid.c_str(),editid.c_str(),0);
break;
case ID_LIST:
switch(HIWORD(wParam)) {
case LBN_SELCHANGE: {
int list = SendMessage(GetDlgItem(hWndDlg,ID_LIST),LB_GETCURSEL,0,0);
int data = SendMessage(GetDlgItem(hWndDlg,ID_LIST),LB_GETITEMDATA,list,0);
std::stringstream str;
str << data;
editid = (string)"a"+str.str();
break;
}
}
break;
}
return true;
case WM_LBUTTONDOWN:
SendMessage(hWndDlg, WM_NCLBUTTONDOWN, HTCAPTION, lParam);
return true;
default:
return false;
}
}
Some variables are defined before this function...Anyway, the "data" variable contains 0 no matter what the id of the mysql row is. My question is, what am I doing incorrectly?
Thanks for any help, it is much appreciated.
-
Code:
SendMessage(GetDlgItem(hWndDlg,ID_LIST), LB_ADDSTRING, 0, (LPARAM)row[1]);
SendMessage(GetDlgItem(hWndDlg,ID_LIST), LB_SETITEMDATA, a, (LPARAM)row[0]);
Where does the value in the variable 'a' come from? It seems that you should be using the return value of LB_ADDSTRING here.
-
for(int a=0;a<mysql_num_rows(result);a++) {
Thats where 'a' comes from. I've tried using:
int x = SendMessage(GetDlgItem(hWndDlg,ID_LIST), LB_ADDSTRING, 0, (LPARAM)row[1]);
And then I would use the 'x' variable in place of the 'a' variable, but that didnt work either.
-
Have you checked the return value from SendMessage to ensure it's a valid, non LB_ERR value when you send either the LB_ADDSTRING or LB_SETITEMDATA messages?
-
I tried this:
int x = SendMessage(...LB_ADDSTRING...);
int y = SendMessage(...LB_SETITEMDATA,x....);
if(x==LB_ERR | y==LB_ERR) MessageBox(0,"Error","Error",0);
And I didnt get the MessageBox error. Any other suggestions? Thanks for your suggestions so far.
-
How stupid can I be? I'm SET(ting)ITEMDATA as a char* variable and GET(ting)ITEMDATA as a int variable. row[0] may contain only integers, but a mysql row is a char* no matter what. I just need to return the data into the data variable like this:
string data = (const char*)SendMessage(...LB_GETITEMDATA...);
Once I did that, it worked. Then I was able to note that I needed to use:
int x = SendMessage(...LB_ADDSTRING);
SendMessage(...LB_SETITEMDATA,x...);
to correctly return the right values. I hope this helps someone besides myself.