如何在 C# 中搜索组合框的所有项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18443146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to search through all items of a combobox in C#?
提问by cabral_007
I have a combobox, and I would like to search through every element in it.
我有一个组合框,我想搜索其中的每个元素。
How can I do this? (also the number of items is not the same everytime, but this is not so important).
我怎样才能做到这一点?(而且项目的数量每次都不一样,但这并不重要)。
I am using c# windows form application.
我正在使用 c# windows 窗体应用程序。
采纳答案by Ehsan
you can do this
你可以这样做
for (int i = 0; i < myComboBox.Items.Count; i++)
{
string value = myComboBox.GetItemText(myComboBox.Items[i]);
}
回答by insomnium_
Use a foreach
loop. It will iterate all your items of ComboBox regardless of their count, e.g.
使用foreach
循环。无论数量如何,它都会迭代您的 ComboBox 的所有项目,例如
foreach(var item in myComboBox.Items)
{
// do something with your item
}