Thread: ListBox Question

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    193

    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.
    Last edited by stickman; 05-21-2006 at 09:32 PM.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    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.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    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.

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    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?
    Last edited by Ken Fitlike; 05-22-2006 at 04:35 PM. Reason: apostrophe disadvantaged
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    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.

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    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.
    Last edited by stickman; 05-23-2006 at 03:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  2. Lame question on dialog controls appearance
    By Templario in forum Windows Programming
    Replies: 2
    Last Post: 03-18-2003, 08:22 PM
  3. Subclassing a listbox and the speed of the postal service
    By SMurf in forum Windows Programming
    Replies: 3
    Last Post: 09-14-2002, 07:25 PM
  4. Getting FULL filename from listbox
    By Garfield in forum Windows Programming
    Replies: 8
    Last Post: 01-27-2002, 08:28 AM
  5. Moving just one part of listbox
    By Garfield in forum Windows Programming
    Replies: 4
    Last Post: 01-22-2002, 04:45 PM