Thread: List Boxes - Detecting user selection

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571

    List Boxes - Detecting user selection

    I've searched through the forums and came up with nothing previous on this topic. I know it's simple I just don't use VC++ Resource editor everyday. I have a simple list box with enumerated display settings. When one is clicked I'd like to know about it and which one etc. Nothing fancy about this I just used LB_ADDSTRING to add each display mode, and used the resource editor for the box. Thanks.

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    50
    In this case, we are interested in LBN_SELCHANGE, which tells us that the selection has been modified by the user. LBN_SELCHANGE is sent via WM_COMMAND but unlike handling the WM_COMMAND from buttons or menu's, which are usually only in response to a click, a list box sends WM_COMMAND for various reasons, and we need a second check to find out what it's telling us. The Notification Code is passed as the HIWORD of wParam, the other half of the parameter that gave us the control ID in the first place:

    case WM_COMMAND:
    switch(LOWORD(wParam))
    {
    case IDC_LIST:
    // It's our listbox, check the notification code
    switch(HIWORD(wParam))
    {
    case LBN_SELCHANGE:
    // Selection changed, do stuff here.
    break;
    }
    break;
    // ... other controls
    }
    break;




    Answer courtesy of the Forger's Win32 API Tutorial

    www.winprog.org


    Then, simply send LB_GETCURSEL to retrieve the item index:

    Send an LB_GETCURSEL message to retrieve the index of the currently selected item, if any, in a single-selection list box.

    To send this message, call the SendMessage function with the following parameters.

    SendMessage(
    (HWND) hWnd, // handle to destination window
    LB_GETCURSEL, // message to send
    (WPARAM) wParam, // not used; must be zero
    (LPARAM) lParam // not used; must be zero
    );
    Parameters
    This message has no parameters.

    Return Values
    In a single-selection list box, the return value is the zero-based index of the currently selected item. If there is no selection, the return value is LB_ERR.

    Now, you need to get the data associated with the item index, which you should know from when you used LB_ADDSTRING to add the item:

    SendMessage(
    (HWND) hWnd, // handle to destination window
    LB_GETITEMDATA, // message to send
    (WPARAM) wParam, // item index
    (LPARAM) lParam // not used; must be zero
    );


    The return value is the value associated with the item, or LB_ERR if an error occurs. If the item is in an owner-drawn list box and was created without the LBS_HASSTRINGS style, this value was in the lParam parameter of the LB_ADDSTRING or LB_INSERTSTRING message that added the item to the list box. Otherwise, it is the value in the lParam of the LB_SETITEMDATA message.


    These last two parts, courtesy of MSDN.

    Thanks!

    Answering this question has been informative.. I knew this stuff but haven't used it, it was interesting to focus on it.

  3. #3
    Unregistered
    Guest
    Originally posted by bluecoder
    [B]Thanks!/B]
    Hey, thank you!

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    That was me, forgot to log in. Anyhow, thanks a lot!

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Hi, I have my own strings I sent in as the LPARAM on the LB_ADDSTRING and I want those values back when I send the message LB_GETITEMDATA. However my compiler informs me that SendDlgItemMessage() returns a long? What am I doing wrong here? It's just a simple string.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    50
    Ahhh you didn't mention you were using dialogs, I assumed you were using regular windows.

    SendMessage() uses only one lParam, a 16-bit parameter, your good ole' everyday length int.

    However, SendDlgItemMessage() uses both the wParam and lParam, which results in a long int return value.

    When you send the message, you have to type caste the index into a long. In other words, on one of your message infos (either the wParam or lParam, I'm not sure), say:

    ..., (WPARAM)myindex, ....

    or ..., ..., (LPARAM)myindex);

    Because you first have to turn your index into the kind of argument SendDlgItemMessage() is expecting. Then you can just say

    int data = SendDlgItemMessage(...........................); and it should return your string the way you want...

    You might have to work with this. I'm learning too and I haven't gotten to my dialog stuff yet.

    Here is what I'm talking about:

    // Since we know ahead of time we're only getting one
    // index, there's no need to allocate an array.

    int index;
    int err = SendMessage(hList, LB_GETSELITEMS, (WPARAM)1, (LPARAM)&index);
    if(err != LB_ERR)
    {
    // Get the data we associated with the item above
    // (the number of times it was added)

    int data = SendMessage(hList, LB_GETITEMDATA, (WPARAM)index, 0);


    This is for regular windows but SendMessage and SendDlgItemMessage work the same way, the dialog just takes one extra argument to identify the control window in the dialog.

    Hope this helps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. link list
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2001, 05:41 AM