retrieving data from listboxes

This is a discussion on retrieving data from listboxes within the Windows Programming forums, part of the Platform Specific Boards category; hello, i'm working on a small app with a listbox, and i'm looking for a way to display the users ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    4

    retrieving data from listboxes

    hello,
    i'm working on a small app with a listbox, and i'm looking for a way to display the users selection. It's a dialog application, and i'm using a single-selection listbox.
    I've tried this, but without success:
    Code:
    	
             case WM_COMMAND:
    		switch(LOWORD(wParam))
    		{
    		case IDC_LIST:
    			switch(HIWORD(wParam))
    			{
    			case LBN_SELCHANGE:
    				LRESULT selection = SendMessage(hWnd, LB_GETTEXT, 0, 0);
    				SetDlgItemText(hWnd, IDC_TEXT, (LPCSTR)selection);
    				break;
    			}
    			break;
    		}
    		break;
    Any ideas what i'm doing wrong?

  2. #2
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    I believe that you are using LB_GETTEXT incorrectly. You need to pass a character buffer in the lParam of SendMessage.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    4
    thanks for your reply,
    I tried your idea, but it seems like i'm doing something else wrong now . here's the new code:
    Code:
    	case WM_COMMAND:
    		switch(LOWORD(wParam))
    		{
    		case IDC_LIST:
    			switch(HIWORD(wParam))
    			{
    			case LBN_SELCHANGE:
    				{
    					char* buf;
    
    					LRESULT len = SendMessage(hWnd, LB_GETTEXTLEN, 0, 0);
    					buf = (char*)GlobalAlloc(GPTR, len+1);
    
    					SendMessage(hWnd, LB_GETTEXT, 0, (LPARAM)buf);
    					SetDlgItemText(hWnd, IDC_TEXT, buf);
    
    					GlobalFree((HANDLE)buf);
    				}
                                    break;
    			}
    			break;
    		}
    		break;

  4. #4
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Is hWnd your listbox?

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    4
    ehm.. i haven't thought about that
    i'm pretty new to this API programming, but i've tried to do as you said, but it still dosen't work . here's the new code:
    Code:
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
            case IDC_LIST:
                switch(HIWORD(wParam))
                {
                case LBN_SELCHANGE:
                    HWND hList = GetDlgItem(hWnd, IDC_LIST);
    
                    char* buf;
    
                    LRESULT len = SendMessage(hList, LB_GETTEXTLEN, 0, 0);
                    buf = (char*)GlobalAlloc(GPTR, len+1);
    
                    SendMessage(hList, LB_GETTEXT, 0, (LPARAM)buf);
                    SetDlgItemText(hWnd, IDC_TEXT, buf);
    
                    GlobalFree((HANDLE)buf);
                    break;
                }
                break;
            }
            break;

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Do you have the LBS_NOTIFY or LBS_STANDARD style? (or will not get the selection change msg).

    Place a break point (or messagebox) to ensure that this section of the callback is receiving messages.



    Try something like

    Code:
    int iSelected = SendMessage( hList, LB_GETCURSEL , 0, 0 );//get current selection
    if(iSelected != LB_ERR) // found a selected item
    {
    	LPTSTR lpsText; 
    	SendMessage(hList, LB_GETTEXT, (WPARAM)iSelected, (LPARAM)lpsText);//add return error check 
    }
    This explains what the LPTSTR data type is.
    MSDN
    "The letter T in a type (for example, TCHAR, LPTSTR) definition designates a generic type that can be compiled for either Windows code pages or Unicode."

    http://msdn.microsoft.com/library/de...icode_28c3.asp
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    4
    thanks, novacain. it all works now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 06:49 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 09:55 PM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 11:28 PM
  4. All u wanted to know about data types&more
    By SAMSAM in forum Windows Programming
    Replies: 6
    Last Post: 03-11-2003, 03:22 PM
  5. Replies: 1
    Last Post: 07-31-2002, 12:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21