Thread: Clicking list box items and functions

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    2

    Question Clicking list box items and functions

    I'm pretty new to all this programming stuff especially winapi. Well to get to it I was wondering if it is possible in c and winapi to call different functions by clicking the item text in a listbox. Sort of like using a list box as a menu. I found this code:

    Code:
    WORD wNotify; /* notification code */
    WORD wID; /* control id */
    HWND hCntrl; /* control handle */
    int nSelection; /* zero-based index of selected listbox item */
    int nLen; /* listbox item number of TCHARS (ie str length) */
    TCHAR *chBuffer; /* for listbox item text */
    wID=LOWORD(wParam);
    wNotify=HIWORD(wParam);
    hCntrl=(HWND)lParam;
    if (hCntrl && wNotify==LBN_SELCHANGE && wID==IDLISTBOX)
    {
    nSelection=SendMessage(hCntrl, LB_GETCURSEL, 0, 0);
    nLen=SendMessage(hCntrl, LB_GETTEXTLEN, nSelection, 0);
    chBuffer=(TCHAR*)malloc((nLen+1)*sizeof(TCHAR));
    
    if (chBuffer)
    {
    SendMessage(hCntrl, LB_GETTEXT, nSelection, (LPARAM)chBuffer);
    /* 'hEdit' is handle to edit control*/
    
    SetWindowText(hwnd,chBuffer);
    
    free(chBuffer);
    }
    }
    It just changes the window text to the selected item text, something like this could be useful to me now. If anyone could help me out with this, or recommend a tutorial or sample code. It would be appreciated.

    Thanks.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    You can set different values for each item as item data, then just get the item data using LB_GETITEMDATA message and switch it:

    Code:
    //while setting up the list
    int item_index;
    
    item_index = SendMessage(hCntrl, LB_ADDSTRING, 0, (LPARAM)"Open file");
    SendMessage(hCntrl, LB_SETITEMDATA, (WPARAM)item_index, (LPARAM)MAKELPARAM(OPEN_FILE, 0));
    
    item_index = SendMessage(hCntrl, LB_ADDSTRING, 0, (LPARAM)"Save file");
    SendMessage(hCntrl, LB_SETITEMDATA, (WPARAM)item_index, (LPARAM)MAKELPARAM(SAVE_FILE, 0)); 
    
    
    
    //and then to respond to action messages
    switch(LOWORD(SendMessage(hCntrl, LB_GETITEMDATA, (WPARAM)nSelection, 0))) {
    	case OPEN_FILE: {
    		call_to_open_file();
    	}
    	break;
    	case SAVE_FILE: {
    		call_to_save_file();
    	}
    	break;
    }
    Of course you will have to declare previously OPEN_FILE and SAVE_FILE (macro or enum). If you have also a standard menu you can set the list item values with the same menu item values, so instead of switch the selected item value you can simulate a menu click sending a WM_COMMAND and the list item value that will correspond to the same action on the standard menu.

    Hope that halps
    Niara

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    2
    Thank you very much Niara, I was away from the computer for a while. It also took me a while to figure that out but I have it working now. Your help is much appreciated. Thanks again.

    Randy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List for items???
    By voidpain() in forum C Programming
    Replies: 7
    Last Post: 08-08-2011, 09:36 PM
  2. how to reverse items in linked list?
    By ilikeapplepie in forum C Programming
    Replies: 8
    Last Post: 04-09-2011, 11:15 PM
  3. Sorting items in a std::list
    By alkis_y3k in forum C++ Programming
    Replies: 7
    Last Post: 01-19-2003, 07:45 AM
  4. modifying items in a linked list?
    By aspand in forum C Programming
    Replies: 3
    Last Post: 06-14-2002, 01:37 PM
  5. sorting list view items
    By cppdude in forum Windows Programming
    Replies: 2
    Last Post: 06-07-2002, 08:21 AM

Tags for this Thread