I have a combobox that I populate like this:
this.reqTypeInput.Items.Add(new RequestType("Label 1", "Value1"));
this.reqTypeInput.Items.Add(new RequestType("Label 2", "value2"));
this.reqTypeInput.Items.Add(new RequestType("Label 3", "value3"));
My RequestType class is:
class RequestType
{
public string Text { get; set; }
public string Value { get; set; }
public RequestType(string text, string val)
{
Text = text;
Value = val;
}
public override string ToString()
{
return Text;
}
}
I have a value, “Value1” for example. How can I set the selectedItem of the combobox to the object {Label 1, Value1}?
I have tried:
this.reqTypeInput.SelectedIndex = this.reqTypeInput.Items.IndexOf("Value1");
If the request types do not change, you could store each RequestType object in a variable first, then set the SelectedItem property of the ComboBox to that variable.
For example:
Then, set it like this: