Resource LISTBOX

This is a discussion on Resource LISTBOX within the Windows Programming forums, part of the Platform Specific Boards category; I didnt find winprog.com's explanation on using Listbox's to well, right now I'm using a listbox that only allows a ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    71

    Resource LISTBOX

    I didnt find winprog.com's explanation on using Listbox's to well, right now I'm using a listbox that only allows a single selection (which is what I want to be using for what im doing).

    I have no problem putting in strings in the listbox, I just can't figure out how im suppose to tell which one the user clicked on.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287
    you can get the selected item sendig 'LVM_GETNEXTITEM' to the list control, setting the WPARAM as -1 and the LPARAM as 'LVNI_FOCUSED|LVNI_SELECTED' (there are other options for that); something like:
    Code:
    int i=-1;
    i=SendMessage(listhwnd,LVM_GETNEXTITEM,(WPARAM)-1,(LPARAM)LVNI_FOCUSED|LVNI_SELECTED);
    by default that returns -1 if there's any problem with the operation.
    if you want to get the item on the 'onclick' event (also other events), you can do it getting the WM_NOTIFY message (procedure's UINT); from it 'filter' the list's id (proc's LOWORD(WPARAM)) and get the LPNMHDR structure (proc's LPARAM); from that struct check the 'code' (UINT) value (if you want the normal click check for the 'NM_CLICK', for the right click->NM_RCLICK, you can google for other events). now send a message to get the selected item, as before if the value is -1 that means that the user has clicked over the list control but no over an item, so exit the function. a basic:
    Code:
    case WM_NOTIFY:
            {
            switch(LOWORD(wParam))
                {
                case LISTIDENTIFYER:
                    {
                    switch(((LPNMHDR)lParam)->code)
                        {
                        case NM_CLICK:
                            {
    			int i=-1;
                            i==SendMessage(listhwnd,LVM_GETNEXTITEM,(WPARAM)-1,(LPARAM)LVNI_FOCUSED|LVNI_SELECTED);
                            if(i>=0)
                                {
                                //ok, you have selected an item
                                }
    //close all brakets :)
    hope that helps
    Niara

  3. #3
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Niara, your solution is for a listview control and not a listbox as the OP has described.

    Listbox controls send notifications to their parent window via parent WM_COMMAND messages; these messages are prefixed LBN_. When a user selects an item in a listbox an LBN_SELCHANGE notification is sent, provided the listbox has the LBS_NOTIFY style. In response to this notification you would send a LB_GETCURSEL message to the listbox to find which item has newly been selected. For example, in your parent's window procedure:
    Code:
    case WM_COMMAND:
      {
      WORD wID,wNotify;
      HWND hCntrl;
    
      wID=LOWORD(wParam);
      wNotify=HIWORD(wParam);
      hCntrl=(HWND)lParam;
    
      if (lParam)
        {
        /*notification is from a control*/
        if (wID==ID_OF_LISTBOX)
          {
          /*notification is from a listbox control with id of 'ID_OF_LISTBOX'*/
          if (wNotify==LBN_SELCHANGE)
            {
            /*a change has been made in the item selected in the listbox*/
            int selected=SendMessage(hCntrl,LB_GETCURSEL,0,0);
            }
          }
        }
      return 0;
      }
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287
    what a big confusion, thanks Ken Fitlike to notify it.

    niara

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    71

    I understand that much (I think)

    But what I don't know is how to use it to make stuff happen (In my case, I have a listbox holding Kinematics , Dynamics, Waves , Projectile Motion etc.)

    Say Kinematics was clicked, How do I make "selected" run on a case? like:

    Code:
    if(selected == kinematics)
    {
        SetDlgItemText(Phys,PHYSICS_EDIT_BOX,Kinematics_Info);
    }

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    I'm not sure what your difficulty is. From what you've posted it seems you may be trying to produce some action as a result of a selection made in a listbox control. If that is the case, then the example code I provided earlier will tell you exactly which item in the listbox has been selected, providing you with its zero-based index value within the list of items in the listbox, at any rate. If all you need is to retrieve the text string associated with the item at that location in the listbox, then use LB_GETTEXT to retrieve it, using SendMessage. If you are looking to take some unique action based on the index value then you could simply use a switch statement to decide on what action to take based on that numerical value; if you're happier using the actual string values then you'd have to use string comparison functions to do that, once you'd retrieved the string corresponding to the index value of the selected item, of course.

    Another possibility since listbox controls permit you to store user data with each listbox item, is to store a function pointer; when a listbox item is selected, you'd simply invoke the function pointer you associated with that item.

    If all of that is wide of the mark then you'll need to provide more information about what it is, exactly, you are trying to do.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    71

    not quite

    what I mean is, once you have:

    Code:
            int selected=SendMessage(hCntrl,LB_GETCURSEL,0,0);
    actually im just really confused... what I want to know is how to make text appear in an edit box depending on what listbox item has been clicked.

    does SendMessage function return the slot location that has just been clicked?

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    71
    I guess I was right x.x, it does return the slot number that I clicked on... thanks got it working =D (you may of said that, but I usually miss key things when reading x.x)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting out a resource manager
    By psychopath in forum Game Programming
    Replies: 1
    Last Post: 11-10-2008, 06:12 PM
  2. CreateProcess with Resource of executable, not the Filename
    By Ktulu in forum Windows Programming
    Replies: 4
    Last Post: 11-04-2006, 12:07 AM
  3. How to cast a ListBox item to an int for a switch statment?
    By Swaine777 in forum C++ Programming
    Replies: 8
    Last Post: 09-26-2004, 08:52 PM
  4. Getting FULL filename from listbox
    By Garfield in forum Windows Programming
    Replies: 8
    Last Post: 01-27-2002, 07:28 AM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM

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