我已经看到很多关于将Array
的内容添加到ComboBox
的内容,但没有反过来。我想将ComboBox
的内容添加到Array
以发送到另一个方法进行处理。如何获取组合框的内容并将它们添加到数组中?
我已经得到了.Items.Count
来确定Array
的大小,但我无法弄清楚如何循环访问ComboBox
中的项目。
我已经看到很多关于将Array
的内容添加到ComboBox
的内容,但没有反过来。我想将ComboBox
的内容添加到Array
以发送到另一个方法进行处理。如何获取组合框的内容并将它们添加到数组中?
我已经得到了.Items.Count
来确定Array
的大小,但我无法弄清楚如何循环访问ComboBox
中的项目。
string[] items = new string[currentComboBox.Items.Count];
for(int i = 0; i < currentComboBox.Items.Count; i++)
{
items[i] = currentComboBox.Items[i].ToString();
}
这个为我工作,感谢您的帮助! –
通过观察你的问题你的意见,你可能想要这个:
var arr = ingredientComboBox.Items.Cast<Object>()
.Select(item => item.ToString()).ToArray();
+1这可能是最简单的。 – lhan
我得到的错误,system.windows.forms.comobobox.objectcollection不包含一个定义选择 –
@CraigSmith aha,'ObjectCollection'不是一个通用的集合。尝试在'Items'后添加'.Cast
'的foreach(VAR在.Items项目)'? – 3aw5TZetdf
public void Ingredients() string [] ingredients = new string [ingredientComboBox.Items.Count]; foreach(var itemComboBox.Items) { for(int i = 0; i
我似乎无法让这看起来像代码在这里,对不起,但类似的东西? –