Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select ComboBox by value in 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;
    }
}

I need to select where Value = 1

like image 373
Reno Avatar asked Dec 12 '22 09:12

Reno


1 Answers

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

like image 86
P.Brian.Mackey Avatar answered Jan 01 '23 11:01

P.Brian.Mackey