Jump to content
  • Advertisement
Sign in to follow this  
kingpinzs

How to get selected string from drop down combo box?

This topic is 4726 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!