Thread: Inserting text into listbox

  1. #1
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273

    Inserting text into listbox

    OK. I'm very new to the whole gui program thing so bear with me.

    I'm writing a program which uses an address book from which a user designates the receipts of a text message. However, I'm having a problem in setting the people in the send-to list. Below is some code I use to initialise the address-book listbox (for testing). The initialisation works fine, but I want to use std::strings instead of char*'s. So I replace all the char*'s with std::strings in the first code segment below and use std::string's .c_str() member function where appropriate and it looks fine on the initial address-book side.

    Now. I select whomever I want to send the message to and click on the 'add' button when I use std::strings the text that's copied over is ... not right, see second link. But with char *'s it's fine ...

    Does anyone know why this happens? The .c_str() returns a const char* so technically the methods I use to populate the lists are near on identical, right? But one works with the copy over while the other doesn't ... All help appreciated.


    Screenshots of char* and std::string builds running:
    Listbox initialization code:
    Code:
    const char *strs[] = { "one", "two", "three", "four", "five", "six" };
    for ( int i=0, id; i<sizeof strs/sizeof(char*); i++ ) {
      id = (int)SendDlgItemMessage( hwnd, IDC_LIST_ADDRESS, LB_ADDSTRING, 0, (LPARAM)strs[i] );
      
      if ( SendDlgItemMessage( hwnd, IDC_LIST_ADDRESS, LB_SETITEMDATA, 
                              (WPARAM)id, (LPARAM)strs[i] ) == LB_ERR )
        MessageBox(hwnd, "Error in LB_GETITEMDATA", "Error", MB_OK);
    }
    Listbox copy code:
    Code:
    HWND hList_a( GetDlgItem( hwnd, IDC_LIST_ADDRESS ) ),
         hList_s( GetDlgItem( hwnd, IDC_LIST_SEND_TO ) ); // Handle to address books
    
    // get the number of selected items
    int count_selected( (int)SendMessage( hList_a, LB_GETSELCOUNT, 0, 0 ) );
    
    if( count_selected != LB_ERR && count_selected != 0 ) {
      // And then allocate room to store the list of selected items.
      int *buf ( (int*)GlobalAlloc( GPTR, sizeof(int) * count_selected ) );
    	
      SendMessage( hList_a, LB_GETSELITEMS, (WPARAM)count_selected, (LPARAM)buf );
    
      for( int i=0, sel_id, iresult; i<count_selected; i++ ) {
        // Add to send-book
        sel_id  = (int)SendDlgItemMessage( hwnd, IDC_LIST_ADDRESS, LB_GETCURSEL,  0,   0 ); 
        iresult = (int)SendDlgItemMessage( hwnd, IDC_LIST_ADDRESS, LB_GETITEMDATA, (WPARAM)buf[i], 0 );
    
        // If not already in the current send-list. 
        if ( SendMessage( hList_s, LB_FINDSTRING, 0, (LPARAM)(iresult) ) == LB_ERR )
          SendDlgItemMessage( hwnd, IDC_LIST_SEND_TO, LB_ADDSTRING, 0, (WPARAM)iresult );
      }
      GlobalFree( buf );
    }

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Shouldn't
    Code:
    SendDlgItemMessage( hwnd, IDC_LIST_SEND_TO, LB_ADDSTRING, 0, (WPARAM)iresult );
    be
    Code:
    SendDlgItemMessage( hwnd, IDC_LIST_SEND_TO, LB_ADDSTRING, 0, (WPARAM)buf );
    ??

  3. #3
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310
    Nop:
    Code:
    SendDlgItemMessage( hwnd, IDC_LIST_SEND_TO, LB_ADDSTRING, 0, (LPARAM)buf );
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Thanks for your post, Yarin, Joelito.

    I've seriously just started so correct me if I'm wrong, but my understanding was that buf was an integer array whose elements stored the values of the selected elements from the first (LHS), list box and the LB_GETITEMDATA dialogue item message returned the value of the current selection to iresult? Am I right? Even if so my naming convention in that case was misleading. Apologies.

    That still doesn't solve the problem, though. Any further ideas?

  5. #5
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    I'm no C++ buff, but it would be nice to see your attempt using the std::strings version.
    Also, unless you specifically want the behaviour defined by GPTR in GlobalAlloc, I would just as soon use the CRT's malloc/free (in many cases CRT is faster than its winAPI coutnerparts, even though in some cases they even call on those same counterparts, but in the very least, at least its a bit more portable).

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    when I use std::strings the text that's copied over is ... not right, see second link. But with char *'s it's fine ...
    Looks like an issue of scope. You got (un)lucky with the char pointers so they seem to still point at something meaningful; apparently when you used std::string::c_str() you saw such rubbish for what it was.

    If all you need do is copy the strings from one listbox to another then just send an LB_GETTEXT message to get the text corresponding to a selected item (the listbox stores the strings for you - LBS_HASSTRINGS is the default style).

    Typically the item data for list items of a listbox are used to store pointers to some information related to the selectable listbox string, such as a pointer to a struct or class, not to the displayed strings themselves. You'd still need to ensure any such class/struct objects thus associated were fully intialised and in scope prior to use, though.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Thanks for your advice, Ken. The LB_GETTEXT message worked fine for the data retrieval. I see what you mean. The char*'s weren't always perfect though. Every now and then I'd get funny info going onto the second list box with the char*'s but decided to just accept them until I figured out why the std::strings weren't workin. Thanks again.

    I'm currently working off the forger's win32 tutorial. There many other gooduns out there?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. inserting text in file
    By Jarwulf in forum C++ Programming
    Replies: 5
    Last Post: 07-01-2008, 09:01 AM
  2. filling a ListBox with variables, not just text?
    By Cielo in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2008, 03:11 AM
  3. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Replies: 1
    Last Post: 07-13-2002, 05:45 PM