Getting index of chosen item in the combo box

This is a discussion on Getting index of chosen item in the combo box within the Windows Programming forums, part of the Platform Specific Boards category; How do you get the index of the chosen item in the combo box... Code: if( HIWORD(wParam)==CBN_SELCHANGE) { int index ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    50

    Getting index of chosen item in the combo box

    How do you get the index of the chosen item in the combo box...

    Code:
    if( HIWORD(wParam)==CBN_SELCHANGE)
    {
     int index =  SendMessage( hwnd, CB_GETCURSEL, (WPARAM) wParam, (LPARAM) lParam ); 
    MessageBox(hwnd,	(LPCTSTR) index , (LPCTSTR)  "The Index is",MB_ICONINFORMATION);
    }
    How would you determine the index number of the combo box? i could get the text but not the index.

    And a general question... when you do combo box (set of conditions) how do you do the conditionals (if...else, switch) do you make the condition thru its name or thru its index?

    Thanks!

  2. #2
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Doesn't CB_GETCURSEL work for you?

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    50
    no it doesn't.

    will it should have a return value of the selected index? it only returns nothing in my case...

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Quote Originally Posted by msdn CB_GETCURSEL
    The return value is the zero-based index of the currently selected item.
    In any event
    Code:
    MessageBox(hwnd, (LPCTSTR) index,(LPCTSTR)  "The Index is",MB_ICONINFORMATION);
    is rubbish: 'index' is a numeric variable, MessageBox requires strings.

    Either use something like
    Code:
    TCHAR buffer[16];
    wsprintf(buffer,_T("%d"),index);
    or use a stringstream to convert that number to a string.

    Try not to cast so much and your compiler should give you important information about what's going wrong; you certainly shouldn't need to cast a string to a string as you have done for the third parameter of your MessageBox call.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    50
    @Ken Fitlike

    Thank you for suggesting that i should convert the number into string.

    Anyway i have solve the problem by using SendDlgItemMessage(...) instead. It seems it needs to know the resource id of the combo box.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. Immediate programming help! Please!
    By xMEGANx in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2008, 12:52 PM
  3. Retail Outlet Managment System - the 4th
    By Presidentofusa in forum C Programming
    Replies: 3
    Last Post: 11-10-2007, 10:44 PM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 04:41 AM
  5. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM

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