How to get selected string from drop down combo box?

Started by
6 comments, last by Dave Hunt 18 years, 4 months ago
How do I get the string that has been selected in the combo box? I tried CB_GETLBTEXT but I am not sure how to send it to a string variable so I can use the string that has been selected. [Edited by - kingpinzs on December 5, 2005 3:13:47 PM]
Advertisement
Is that another comand that I would have to use to see what iteam has been selected in the combo box?
Try this:
char pFilename[_MAX_PATH];HWND hComboBox = GetDlgItem( m_hWnd, IDC_COMBOBOX );int nIndex = SendMessage( hComboBox , CB_GETCURSEL, 0, 0 );SendMessage( hComboBox , CB_GETLBTEXT, nIndex, (LPARAM)pFilename );


Good luck. Illco.
Have a look at GetWindowText. You pass the window handle of your combobox, a char array, and the size of the array. It will populate your char array with a null-terminated string containing the currently selected text.
Thanks for every ones help it is most apertiated.

this worked perfectley

int nIndex = SendMessage( hComboBox , CB_GETCURSEL, 0, 0 );
SendMessage( hComboBox , CB_GETLBTEXT, nIndex, (LPARAM)pFilename );


switch ( LOWORD( wParam ) ){    case ComboList:      {        if( HIWORD( wParam ) == CBN_SELCHANGE )        {                 int nIndex = SendMessage( hwndCombo1 , CB_GETCURSEL, 0, 0 );             SendMessage(hwndCombo1,CB_GETLBTEXT, nIndex,(LPARAM)SelString.c_str());                        MessageBox(hwnd,SelString.c_str(),"Error",0);                  }      }break;}


I been looking all over google for hours and did not find anything like it.

I think it would be cool if this could be added to faq or something so if any one else wants to know how to test for cahnge and get text from a combo box drop down this is how you could do it.

Strange how you couldn't find the information. Are you aware of MSDN? Usually you can just paste the Win32 API call or message into google and it will lead you there. The above code for the combobox is fully specified by the following two pages and anyone can use it to formulate the right code:

CB_GETCURSEL

CB_GETLBTEXT

Greetz. Illco.
I found both those commands but did not have an example on how to use them.
Quote:Original post by kingpinz
SendMessage(hwndCombo1,CB_GETLBTEXT, nIndex,(LPARAM)SelString.c_str());



That is a very bad idea. You should NEVER pass the value of std::string::c_str() as an output variable. The address returned from c_str() should only be used as a read-only pointer. You can't assume that enough space has been allocated to hold the string being returned, nor can you even assume that the c_str() isn't temporary. Also, the std::string's internal data members don't get updated correctly. There are a lot of other reasons why this is bad, but that's enough.

You should use a character array for the output parameter:
char tmpMsg[128];std::string myString;SendMessage(hwndCombo1,CB_GETLBTEXT,nIndex,(LPARAM)tmpMsg);myString = tmpMsg;

This topic is closed to new replies.

Advertisement