Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding a string to a combobox via the edit control

Status
Not open for further replies.

HyperEngineer

Programmer
May 8, 2002
190
US
I have a problem getting the string out of the edit control of a drop down combo box. I can into the edit control but can't seem to do anything else with it. Here is some code:

{
// TODO: Add your control notification handler code here

// Check that a string is entered
// Get the current selection index
CString strSelected;
CString strAdd;
int nIndex = m_cboString.GetCurSel();
if (nIndex == CB_ERR)
{

m_cboString.GetLBText(-1,strAdd);
m_cboString.AddString(strAdd);
m_cboString.SelectString(0,strAdd);
nIndex = m_cboString.GetCurSel();
}
if (nIndex != CB_ERR)
{
// Store the selection
m_cboString.GetLBText(nIndex, strSelected);

if ((strSelected != ""))
{
AfxMessageBox("valid string");
}
else
{
AfxMessageBox("invalid string");
}
}
}
This does not work. GetLBText does not like a negative index. Would appreciate some help.
thanks,
HyperEngineer
 
Yeah i don't know who told you to put a -1 in there. Here is an excerpt from the documentation for that function.

Contains the zero-based index of the list-box string to be copied.

You need to read the document for the functions you are using. Otherwise how do you know what to put in the parameters?

-pete

 
Pete,
I did read the docs. MSDN can be a bit convoluted at times. I realize it does not say to use -1, but other functions connected with this did use -1, and no definitions of why. Found it in books. However, it also does not tell you how to get the string from the edit box into the list box. I can type into it but can't find anything in the documentation that tells me how to add it to the list. The code was just to show what I had tried. But my problem still remains. How to get the edit control string into the list control of the combo box.

thanks,
Hyper
 
So you need to perform two separate operations yes? Put a new string in a combobox and get the data in the edit box of the combobox.

Now you probably know CComboBox.AddString() puts data into a combobox but there is said it anyway.

To getthe data from the edit control of a combobox variable named "myComboBox", you might do something like this:

Code:
 CString sText;
:::GetWindowText( myComboBox.m_hWnd, sText.GetBuffer( 256), 255);
sText.ReleaseBuffer();
// now the text is in sText;

Simple yes? Now comes the “Ohhh” part. How do you know if the user typed a new string in that needs to be added to the combobox or if they just selected an entry that is already in there?

hope that helps
-pete
 
Thanks Pete,
Looking at your example and going through MSDN to better understand it jogged my memory. I found an easier way. For the code segment when nIndex == CB_ERR I use the UpdateData() function and I have a public variable m_MyCombo.

{
UpdateData(TRUE);
m_cboMyCombo.AddString(m_strMyCombo);
m_cboMyCombo.SelectString(0, m_strMyCombo);
}

Now I can add validation code to make sure that the string is valid and not a duplicate.

Thanks again,
HyperEngineer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top