How to add list box items to a combo box at run time by user ?

Hi !
I'm using VC++ 6.0 and have an app with combo boxes, giving the user several choices to choose from. What I would like to know is if  the user decides to type in new text into the combo box and he would like to have this selection added to the list already available from the box, how do I do this ? I could put a button at the right of each combo box and when he clicked it, this would mean he wanted to store the text as a new selection in the combo box. Could someone give me the mecanics on how to go about this please ? Thanks!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You can do this:

Add a control variable to your combo box. In this example my control is called mycombobox_control. Then place this code in the button handler you have next to each combobox to signal the user wants to add the string:

void CMyTestDlg::OnBnClickedButton1()
    CString new_string; // this is where we'll store the users new entry
    mycombobox_control.GetWindowText(new_string); // this stores what they typed into "new_string"
    mycombobox_control.AddString(new_string); // this adds that string to the list of combo box items.

That's all. After you click on the button you'll see whatever the user typed in the field appear in the list of options. Be aware that after you close your application, the added strings will not "persist". If you want the strings they added to be available on the next run, you'll have to save those strings somehow to be read in the next run (ie. using the registry).

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I would assume that when you can edit the text a simple enter signals that the entry in the edit box is accepted and should be used. For me it would therefor be more intuitive to use no extra button but the enter key. Unfortunatly handling the enter key in a normal edit box is not so easy. Some examples at Codeguru might give some guidance.
AndyAinscowFreelance programmer / ConsultantCommented:
Create your own control based on a CComboBox.
Trap the KillFocus event and have code like the following
      CString s;
      int iCur = GetCurSel();      //is there a valid selection at present?

      int i = FindString(-1, s);
      if(i == CB_ERR)            //It's a new string
//Ask for user confirmation? or just add
AddString(s);  //This should add onto the list of entries
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

pigeonbrAuthor Commented:

I guess I shold have specified that I do want the new strings to persist...there is no easy way of adding a new string the the existing list of data and have it persist with the rest ?? Really ??  Then the users specific choices must be painstakingly decided by him and coded into the datat list for every app has about 200 combo boxes...what a job...I await any alternatives...thanks :)
AndyAinscowFreelance programmer / ConsultantCommented:
My earlier post does not require any extra user input (no button push), it responds to the user entering a new string directly.
Do I understand you have 200 combos and adding a string into 1 of them should make it available to all the other 199?

If so have a central function that you pass a string to and this pushes it into each combo in your app.

If you want it to persist between closing the app and re-opening then store the strings to a file/database and read them in at app startup.
pigeonbrAuthor Commented:

Hi ..ok I will check it out visual setup is long done...over 200 combo boxes on 9 different property pages...I would have to redo them all then if this is the only solution, right ?

""Do I understand you have 200 combos and adding a string into 1 of them should make it available to all the other 199?"", just to the one relavent combo box that the text is entered into, not the whole gang.

""If you want it to persist between closing the app and re-opening then store the strings to a file/database and read them in at app startup. ""....ok, so I save making a push button then..I see...but still have to save the new strings manually....that invloves alot of extra work, and time...I will see if the customer wants to $$ for, looks like there is no other way...I will await any other ideas and try yours out...thanks ! Back to you later.

AndyAinscowFreelance programmer / ConsultantCommented:
I am not talking of changing your visual interface.
If you have (in header files) 200 times CComboBox m_cmboXXX you would change each to CMyComboBox m_cmboXXX.
The KillFocus event handler is in your customised CMyComboBox class.

You can also then code a load/save routine in this class.  The loading could be done by eg an overridden OnCreate event handler (or PreSubClass - however it is spelt).  The saving in possibly OnPostNcDestroy or other event fired during the destruction of the combo.
That doesn't require a tremendous change.

There's not too much extra work if you want to save the changes to a file or the registry after hitting your "Accept" button. You can simply create an array of the values in the combo boxes, check for which ones have been changed, then write back the ones that have been altered when they hit "Accept". I'm not sure if you're still working on this, anyways, if you have questions on that let me know. Saving the changes in a database is also equally easy.
pigeonbrAuthor Commented:
After looking at this from several angles, looks like what I wanted to do (add text choice to list already in combo box) cannot be done. Thanks for the help...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.