如何在组合框中获取索引值?

时间:2014-12-09 06:55:01

标签: c# combobox

public class CustomComboItem
{
    public Double CodeValue { get; set; }
    public String DisplayName { get; set; }
    public String Description { get; set; }
  }

for (int i = 0; i < locCnt; ++i)
 { //I am setting member variable of  CustomComboItem
   // and add to the combobox
   ComboBox1.Items.Add(customComboItem1);
 }

如果我需要按CodeValue搜索,那么如何获取comboBox的索引。

2 个答案:

答案 0 :(得分:0)

使用foreach

Int i = 0;

foreach(var item in myComboBox.Items)
{
    if(item.CodeValue = SearchCodeValue)
    {
        return i  // result index
    }
    else
    {
        i = i + 1;
    }
}

答案 1 :(得分:0)

您可以尝试:

var index = Array.FindIndex<CustomComboItem>(ComboBox1.Items.Cast<CustomComboItem>().ToArray<CustomComboItem>(), item => item.CodeValue == SearchCodeValue);