因為ComboxItem是Object對象,而控件自身沒有Value屬性.所以,需要自定義一個類,用其對象來存儲Text,Value.
public class ComboxItem
{
private string text;
private string values;
public string Text
{
get { return this.text; }
set { this.text = value; }
}
public string Values
{
get { return this.values; }
set { this.values = value; }
}
public ComboxItem(string _Text, string _Values)
{
Text = _Text;
Values = _Values;
}
public override string ToString()
{
return Text;
}
}
賦值示例一:
cbDictData.Items.Add(new ComboxItem("用戶類型", "D1"));
cbDictData.Items.Add(new ComboxItem("地區字典", "D2"));
cbDictData.Items.Add(new ComboxItem("區域字典", "D3"));
賦值示例二:
ComboxItem[] values = {
new ComboxItem("用戶類型", "D1"),
new ComboxItem("地區字典", "D2"),
new ComboxItem("區域字典", "D3")
};
cbDictData.Items.AddRange(values);
取值示例:
string strDict = ((ComboxItem)cbDictData.SelectedItem).Values;