As far as I know, combo-boxes in Windows Forms can only hold one value. I needed a text and an Index so i created this little class:
public class ComboboxItem {
public string Text { get; set; }
public object Value { get; set; }
public override string ToString()
{
return Text;
}
}
I add an Item to the combo-box as following:
ComboboxItem item = new ComboboxItem()
{
Text = select.Item1,
Value = select.Item2
};
this.comboBoxSelektion.Items.Add(item);
Now to my question: How do I set the combobox to specific item?
I tried this, but that didn’t work:
this.comboBoxSelektion.SelectedItem = new ComboboxItem() { Text = "Text", Value = 1};
The last code sample you provided doesn’t work, because the item in the
ComboBoxand the item you create vianeware different instances (= memory references) which are not the same (two different memory pointers) even though they are equal (their members have the same values). Just because two objects contain the same data doesn’t make them the same object – it makes them two different objects that are equal.That’s why usually there’s a big difference between
o1 == o2ando1.Equals(o2);.Examples:
What you need to do is find the same instance you added to the list. You could try the following:
This selects the first item from the items assigned to the
ComboBoxthe value of which is1and sets it as the selected item. If there’s no such item,nullis set as theSelectedItem.