Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 323
  • Last Modified:

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!
0
pigeonbr
Asked:
pigeonbr
  • 3
  • 3
  • 2
  • +1
1 Solution
 
ragin_cajunCommented:
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).
0
 
SteHCommented:
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.
0
 
AndyAinscowCommented:
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?

      GetWindowText(s);
      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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
pigeonbrAuthor Commented:
rajin_cajin;

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 control....my app has about 200 combo boxes...what a job...I await any alternatives...thanks :)
0
 
AndyAinscowCommented:
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.
0
 
pigeonbrAuthor Commented:
AndyAinscow;

Hi ..ok I will check it out Andy...my 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?""....no, 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 this...so, looks like there is no other way...I will await any other ideas and try yours out...thanks ! Back to you later.


0
 
AndyAinscowCommented:
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.
0
 
ragin_cajunCommented:
Hey,

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.
0
 
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...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now