根据键值对设置组合框的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 对象存储在一个变量中,然后设置 SelectedItem 属性的 ComboBox 到该变量.
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屋!
查看全文