Solved

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

Posted on 2003-11-07
9
315 Views
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!
0
Comment
Question by:pigeonbr
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 1

Accepted Solution

by:
ragin_cajun earned 250 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).
0
 
LVL 13

Expert Comment

by:SteH
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.
0
 
LVL 44

Expert Comment

by:AndyAinscow
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?

      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
 

Author Comment

by:pigeonbr
ID: 9707090
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 44

Expert Comment

by:AndyAinscow
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.
0
 

Author Comment

by:pigeonbr
ID: 9710475
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
 
LVL 44

Expert Comment

by:AndyAinscow
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.
0
 
LVL 1

Expert Comment

by:ragin_cajun
ID: 9716947
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
 

Author Comment

by:pigeonbr
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...
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Installing Microsoft R server on REDHAT or SUSE Linux 4 125
ORA-01403: no data found 43 83
canBalance challenge 34 65
mapAB Challlenge 35 89
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now