Craig Smith
Craig Smith

Reputation: 631

How do you take the contents of a combo box and add them to an array?

I've seen a lot about adding the contents of an Array to a ComboBox, but not the other way around. I would like to take the contents of a ComboBox add them to an Array to be sent to another method for processing.

I've already got the .Items.Count to determine the size of the Array, but I can't figure out how to cycle through the items in the ComboBox.

Upvotes: 6

Views: 12413

Answers (2)

yohannist
yohannist

Reputation: 4204

   string[] items = new string[currentComboBox.Items.Count];

   for(int i = 0; i < currentComboBox.Items.Count; i++)
   {
       items[i] = currentComboBox.Items[i].ToString();
   }

Upvotes: 2

Magnus
Magnus

Reputation: 46909

From looking at your comments on your question you probably want this:

var arr = ingredientComboBox.Items.Cast<Object>()
          .Select(item => item.ToString()).ToArray();

Upvotes: 11

Related Questions