在Winforms中按值选择ComboBox [英] Select ComboBox by value in winforms
本文介绍了在Winforms中按值选择ComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在WinForms中按值选择组合框?我正在这样设置组合框:
How to select combo box by value in WinForms? I am setting combobox like that:
ComboboxItem item = new ComboboxItem();
item.Text = "Test";
item.Value = 1;
cmbComboBox.Items.Add(item);
internal class ComboboxItem
{
public string Text { get; set; }
public object Value { get; set; }
public override string ToString()
{
return Text;
}
}
我需要选择Value = 1
I need to select where Value = 1
推荐答案
因为 ObjectCollection 仅实现了 IEnumerable< T>
c $ c> IEnumerable 您不能使用LINQ标准查询运算符。但是,只需使用 Cast<
作弊,以获得LINQ友好的可查询集合:
Because ObjectCollection does not implement the generic IEnumerable<T>
only IEnumerable
you can't use LINQ standard query operators. However, just cheat a little bit by using Cast<T>
to obtain a LINQ friendly queryable collection:
var result = comboBox1.Items.Cast< ComboBoxItem>()。Where(i =>(int.Parse(i.Value.ToString()))== 1);
这篇关于在Winforms中按值选择ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文