基于键,值对设置组合框的selectedItem。 [英] Set the selecteditem of a combobox based on key,value pair.
本文介绍了基于键,值对设置组合框的selectedItem。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个组合框,我这样填充:
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"));
我的RequestType类是:
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;
}
}
我有一个值,例如Value1。如何将组合框的selectedItem设置为对象{Label 1,Value1}?
I have a value, "Value1" for example. How can I set the selectedItem of the combobox to the object {Label 1, Value1}?
我试过:
this.reqTypeInput.SelectedIndex = this.reqTypeInput.Items.IndexOf("Value1");
推荐答案
如果请求类型不更改,请先在变量中指定每个RequestType对象,然后设置
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.
例如:
RequestType type1 = New RequestType("Label 1", "Value 1");
RequestType type2 = New RequestType("Label 2", "Value 2");
reqTypeInput.Items.Add(type1);
reqTypeInput.Items.Add(type2);
然后,设置如下:
reqTypeInput.SelectedItem = type2;
这篇关于基于键,值对设置组合框的selectedItem。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文