0

I have a windows form that I want to be populated with X number of comboboxes at compile time, X being a varying number.

I have a List with all the values for the comboboxes but since the user must be able to select a differnet item for each combobox I would need a new binding list for each one but since I don't know how many I would have to begin with, I would need to create them in a loop. In order to do so I thought to lookup how to pass a string to a variable name, since they all need unique names but I had no luck.

Could anyone please tell me how I can create/initialise variables in a loop with unique names or an alternate way of achieving my goal.

mathgenius
  • 503
  • 1
  • 6
  • 21
  • 3
    Why you need variable? just set each dynamic combobox data source to a copy of your list. `dynamicCombo.DataSource = sourceList.ToList();`. – Reza Aghaei May 14 '16 at 00:21
  • You will find this post helpful [Multiple ComboBox bind to a Single List - Issue: When I choose an item, all combo boxes changes](http://stackoverflow.com/questions/35864906/bind-multiple-combobox-to-a-single-list-issue-when-i-choose-an-item-all-comb/35865838#35865838) In that post I described 3 simple solutions to bind multiple combo boxes to a single list. – Reza Aghaei May 14 '16 at 13:00
  • @RezaAghaei, thank you very much! Just to note your first comment solved my conundrum. – mathgenius May 14 '16 at 18:16

1 Answers1

0

you can try creating a combobox list
and initialze it at runtime when you know X
then add it to the controls collection

List<ComboBox> l = new List<ComboBox>();
for(int i=0;i<X;i++)
{
    ComboBox cb = new ComboBox();
    //add items to cb from your list
    //cb.items.add("your item")
    l.add(cb);
    //they will be created on top of each other
    //you can use this
    cb.Location = new Point(0,i*20);
    controls.add(cb);
}

then you can access the combo boxes by using the list

l[0].SelectedIndex
Modar Na
  • 49
  • 4
  • I see, you are basically adding each element of the source list to the combobox. Thank you for your input, but I believe @Reza-Aghaei's answer/comment to be better. – mathgenius May 14 '16 at 10:36