Home > DeveloperSection > Forums > Programmatically storing a list into my combobox's item collection
Chintoo Semi

Total Post:135

Posted on    January-27-2014 11:41 PM

 C# C# 

 1 Reply(s)
 921  View(s)
Rate this:

I am working on a Windows Form right now, specifically, a combo box.

I have two List<string>. Let's say the first List are names, and the second List are ages.

How does one store the entire first List into the collection, so I can use the comboBox to access them?

Upon clicking on an item in this collection, for example, the fifth name, how do I get the index of this item?

So far, I have created my form, and laid the skeleton of the win forms application, but the part of storing the list into the collection is stopping me from moving forward 

 thank you.

Pravesh Singh

Total Post:412

Posted on    January-28-2014 12:49 AM

Hi Chintoo,

For the list of strings it can be done like this

comboBox.DataSource = myList;

If you don't want to tie your list in Datasource, still one line

comboBox.DataSource = myList.ToArray().Clone();

Now, if this is list of Strings you can get the whole string

string s = (string)comboBox.SelectedItem; // vs comboBox.SelectedIndex

Don't want to miss updates? Please click the below button!

Follow MindStick