Get current selection from combobox

This is a discussion on Get current selection from combobox within the Windows Programming forums, part of the Platform Specific Boards category; I cannot get the value of the item selected in a combobox..... whats wrong? is it the integer number? should ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    37

    Get current selection from combobox

    I cannot get the value of the item selected in a combobox.....
    whats wrong? is it the integer number? should it be a pointer, an array?
    I tried it all but non seems to work

    Code:
    
    switch (Message)
        { 
           case WM_COMMAND:
             {
    	switch(LOWORD(wParam))
    	{
                                               
    	  case IDC_CON:
    	  { 
                         number = SendMessage(hCombo,CB_GETCURSEL, 0,0); //Global variable
                        
                      }break;
                    } 
            }break;
         }
    //
    Note: it is the first time I am using comboboxes and I have referrenced from MSDN

    Compiler : DEV C++
    OS: WIN XP
    Last edited by Sober; 05-04-2007 at 09:48 AM.

  2. #2
    Registered User
    Join Date
    Apr 2007
    Posts
    37
    I know its lame...but I badly need to get the combobox to work...I have looked on the net for hours without success...most examples found are for ownerdran combo boxes...can anyone guide my search?

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287
    Read another time the references on MSDN (or in the Win32 Prog.Ref.), it says about the return value of the 'CB_GETCURSEL' message:
    Code:
    The return value is the zero-based index of the currently selected item. If 
    no item is selected, it is CB_ERR.
    As it says the value is the index of the item, so if you want to get the text of that item you'll have to use

    Code:
    CB_GETLBTEXT  
    wParam = (WPARAM) index;                //item index, your case is 'number'
    lParam = (LPARAM) (LPCSTR) lpszBuffer;  //address of buffer
    That should work

    Niara

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    37
    No I need the zero-based index of the item that has been selected. Thats why I used GETCURSEL. btw am using a drop down list style combobox..

    Code:
    lResult = SendMessage(      // returns LRESULT in lResult  
    HWND) hWndControl,      // handle to destination control  
    (UINT) CB_GETCURSEL,      // message ID   
    (WPARAM) wParam,      // = 0; not used, must be zero  
    (LPARAM) lParam );      // = 0; not used, must be zero
    The return value is the zero-based index of the currently selected item. If no item is selected, it is CB_ERR.

    I need that index..

  5. #5
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    and so what is it returning?

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    37
    Apparently nothing..I used trhe return value, which I stored in the integer variable, to display the number of players of the game am creating...

    BUt since I initially assigned the value 3 to number, I get only 3 players. When a different number is selected from the combobox the players remain to 3....But am sure there's no problem with my coding for displaying the number of players since when I alter the inital value to 4 or 2 in my code, I get the expected results...So am confused about the return value for CB_GETCURSEL..


    Any clue?

  7. #7
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    If the return value is a valid index (ie not CB_ERR as previously mentioned by Niara) then it's -er- a valid index. Make sure you send the relevant control the CB_GETCURSEL message at an appropriate point in your program - just before you need to use it seems to be best. Test the return from SendMessage, though - use a message box to display the index; if nothing else it will provide you with some diagnostic regarding any problems at that point.

    In short, given your description thus far, it seems to be an issue of program flow/execution order rather than an error(assuming you've made sure that the combobox handle used with SendMessage is valid?).
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    37
    Code:
    BOOL CALLBACK DlgNG(HWND dlgst, UINT Message, WPARAM wParam, LPARAM lParam)
    {
    HWND  hCombo;
    TCHAR chTxt[3];    //text to add to combobox control(s) 
    char *sel;    
    	switch(Message)
    	{
    		case WM_INITDIALOG:
             //create a drop down combobox  
                     hCombo=CreateWindowEx(0,                                     //more or 'extended' styles
                                  TEXT("COMBOBOX"),                      //'class' of control to create
                                  NULL,                                  //the control caption
                                  WS_CHILD|WS_VISIBLE|CBS_DROPDOWNLIST,      //control style: how it looks
                                  190,                                    //control position: left
                                  40,                                   //control position: top
                                  50,                                   //control width
                                  90,                                    //control height
                                  dlgst,                                  //parent window handle
                                  (HMENU)0x99,                                  //control's ID
                                  g_hInst,                               //application instance
                                  NULL); 
                                  
                                      //add a string to the drop down combobox
            lstrcpy(chTxt,TEXT("2"));
            SendMessage(hCombo,CB_ADDSTRING,0,(LPARAM)chTxt); 
            lstrcpy(chTxt,TEXT("3"));
            SendMessage(hCombo,CB_ADDSTRING,1,(LPARAM)chTxt); 
            lstrcpy(chTxt,TEXT("4"));
            SendMessage(hCombo,CB_ADDSTRING,2,(LPARAM)chTxt);              
            	return TRUE;                      
                                  
                                  
    	
    		case WM_COMMAND:
    			switch(LOWORD(wParam))
    			{
    				case ID_ST:
    				sel=SendMessage(hCombo,CB_GETCURSEL,(WPARAM)0,(LPARAM)0);
    				MessageBox(hwnd,sel,"start?",MB_OK);
    				EndDialog(dlgst, ID_ST);
    				break;
    				case ID_CL:
    					EndDialog(dlgst, ID_CL);
    				break;
    			}
    		break;
    		default:
    			return FALSE;
    	}
    	return TRUE;
    }
    Here is the coding....actualy I have tried using a pointer to char and to int to get the return value...but got this warning
    Objects.c: In function `DlgNG':
    Objects.c:481: warning: assignment makes pointer from integer without a cast
    ....wats wrong?
    Last edited by Sober; 05-05-2007 at 09:35 AM.

  9. #9
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    It is not possible that if you have a variable lResult and you have a statement like lResult = ... nothing happens with lResult. So if it's still 3, the selection index is 3.

  10. #10
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    You need to convert the numerical index to a string prior to using it in a message box:
    Code:
    TCHAR buffer[64];
    wsprintf(buffer,_T("%d"),sel);
    MessageBox(0,buffer,_T("start?"),MB_OK);
    #include <tchar.h> after windows.h to use the _T macro or just use the TEXT macro instead.

    edit: And declare 'sel' static (ie static int sel; )for proper scope.

    edit2: biffed by Pronecracker.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    37
    On any selection made I get '0'
    Whats happening?

    Code:
    BOOL CALLBACK DlgNG(HWND dlgst, UINT Message, WPARAM wParam, LPARAM lParam)
    {
    HWND  hCombo;
    TCHAR chTxt[3];    //text to add to combobox control(s) 
    TCHAR buffer[60];
    
    static int sel;    
    	switch(Message)
    	{
    		case WM_INITDIALOG:
             //create a drop down combobox  
                     hCombo=CreateWindowEx(0,                                     //more or 'extended' styles
                                  TEXT("COMBOBOX"),                      //'class' of control to create
                                  NULL,                                  //the control caption
                                  WS_CHILD|WS_VISIBLE|CBS_DROPDOWNLIST,      //control style: how it looks
                                  190,                                    //control position: left
                                  40,                                   //control position: top
                                  50,                                   //control width
                                  90,                                    //control height
                                  dlgst,                                  //parent window handle
                                  (HMENU)0x99,                                  //control's ID
                                  g_hInst,                               //application instance
                                  NULL); 
                                  
                                      //add a string to the drop down combobox
            lstrcpy(chTxt,TEXT("2"));
            SendMessage(hCombo,CB_ADDSTRING,0,(LPARAM)chTxt); 
            
            lstrcpy(chTxt,TEXT("3"));
            SendMessage(hCombo,CB_ADDSTRING,1,(LPARAM)chTxt); 
            
            lstrcpy(chTxt,TEXT("4"));
            SendMessage(hCombo,CB_ADDSTRING,2,(LPARAM)chTxt); 
                         
            	return TRUE;                      
                                  
                                  
    	
    		case WM_COMMAND:
    			switch(LOWORD(wParam))
    			{
    				case ID_ST:
    				sel=SendMessage(hCombo,CB_GETCURSEL,(WPARAM)0,(LPARAM)0);
    				//MessageBox(hwnd,sel,"start?",MB_OK);
                    wsprintf(buffer,TEXT("&#37;d"),sel);
                    MessageBox(dlgst,buffer,TEXT("start?"),MB_OK);
    				//EndDialog(dlgst, ID_ST);
    				break;
    				case ID_CL:
    					EndDialog(dlgst, ID_CL);
    				break;
    			}
    		break;
    		default:
    			return FALSE;
    	}
    	return TRUE;
    }
    Last edited by Sober; 05-05-2007 at 09:54 AM.

  12. #12
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Sorry, I had to leave and do things - I meant to point out explicitly that you need to declare any variables that you expect to retain their values between function calls as static within your window procedure. Your combobox window handle is only a valid window handle within the context of the WM_CREATE, assuming CreateWindowEx does its job. Later, when a WM_COMMAND is handled it's not valid; I'm surprised you're getting anything short of rubbish from the CB_GETCURSEL. Confirm this for yourself by using IsWindow with the combobox handle in your WM_COMMAND handler.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  13. #13
    Registered User
    Join Date
    Apr 2007
    Posts
    37
    Great .Thanks. I didn't thought declaring static variables for a function would make such a difference..

  14. #14
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    Quote Originally Posted by Sober View Post
    Great .Thanks. I didn't thought declaring static variables for a function would make such a difference..
    Well, 'such a difference', it makes only the difference that they are not deallocated when the function returns and keep their values.

  15. #15
    Registered User
    Join Date
    Apr 2007
    Posts
    37
    Yeah got that now.But I used to think memory is deallocated when application is exited..process ended..I got to revise the fundamentals

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Immediate programming help! Please!
    By xMEGANx in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2008, 12:52 PM
  2. Problem with simple case statements
    By shoobsie in forum C Programming
    Replies: 2
    Last Post: 05-08-2006, 09:39 AM
  3. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM
  4. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 03:53 PM
  5. HelpMePlease
    By Prncess100 in forum C++ Programming
    Replies: 6
    Last Post: 12-11-2002, 02:02 PM

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