如何在 WinForms 中按值选择组合框?我正在这样设置组合框:
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 的地方
最佳答案
因为 ObjectCollection没有实现通用 IEnumerable<T>
只有IEnumerable
您不能使用 LINQ 标准查询运算符。但是,通过使用 Cast<T>
来作弊获取 LINQ 友好的可查询集合:
var result = comboBox1.Items.Cast<ComboBoxItem>().Where(i => (int.Parse(i.Value.ToString())) == 1);
关于c# - 在 winforms 中按值选择 ComboBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20104646/