Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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

Posted on 2003-11-07
Medium Priority
Last Modified: 2013-11-20
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!
Question by:pigeonbr
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1

Accepted Solution

ragin_cajun earned 1000 total points
ID: 9701271
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).
LVL 13

Expert Comment

ID: 9701329
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.
LVL 44

Expert Comment

ID: 9701940
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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.


Author Comment

ID: 9707090

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 :)
LVL 44

Expert Comment

ID: 9710057
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.

Author Comment

ID: 9710475

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.

LVL 44

Expert Comment

ID: 9712743
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.

Expert Comment

ID: 9716947

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.

Author Comment

ID: 9806188
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...

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question