Select ComboBox by value in winforms

…衆ロ難τιáo~ 提交于 2020-01-24 09:01:07

问题


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;
    }
}

I need to select where Value = 1


回答1:


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);



来源:https://stackoverflow.com/questions/20104646/select-combobox-by-value-in-winforms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!