Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to add 40 strings to combobox. I am using dialogbased mfc applications. I have 20 comboboxes on dialog and I want to add same 40 strings to all the dialogboxes.
How i can do that? I can add strings to each combobox separatly but the program will become lengthy. Is their any other method? Please Helep me.

Thanks in advance.
Posted
Comments
Richard MacCutchan 18-Dec-12 13:53pm    
Take a look at your design and ask yourself why you need 20 combo boxes on one dialog, particularly as they all contain the same 40 strings.
peoria123 18-Dec-12 14:18pm    
They contain the same strings...but selected string for comboobox is different..for 1st combobox "string1" is selected for 2nd combobox "string2" is selected and so on.

Adding 40 strings to 20 combobox should NOT take much time; there must be some other issues somewhere.

You should at least have an array with the ID of each combobox (or maybe an array of the combobox themselves) for quicker access.

I just tried something like this, and it seems to be working fine.

const int numberOfStrings = 40;
CString arrayOfString[numberOfStrings ];

const int numberOfComboBox = 20;
UINT arrayOfId[numberOfComboBox ];
// fill the array with the ID of the combobox
//...


for ( unsigned int comboIndex = 0; comboIndex < numberOfComboBox; comboIndex++ )
{
  CComboBox* combo = (CComboBox*) GetDlgItem( arrayOfId[comboIndex] );
  for (unsigned int stringIndex = 0; stringIndex < numberOfStrings; stringIndex++ 
  {
    combo->AddString(arrayOfString[stringIndex]);
  }
}


But as Richard wrote, there should be a better way of doing what you want to do without have to have 20+ combo in a single form.

Max.
 
Share this answer
 
You haven't stated if the strings are static information (eg. "Jan", "Feb", "Mar", ...) or dynamic information that can change each time you activate the dlg. If the information is static, you can add the strings in the resource editor for the dialog. The strings are then compiled into the resource data and don't show up in your C++ code. Doing this is a little faster than manually adding at runtime.

Also, if the information is 'usually' one set of strings or some of the information is always there, you can set the default info in the resource editor and either add to it ("AddString()") or replace it ("ResetContent()") in code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900