110,579
社区成员
发帖
与我相关
我的任务
分享
private List<TextValue> list = new List<TextValue>();
/// <summary>
/// 为下拉列表绑定数据
/// </summary>
private void BindComboBox()
{
list.Add(new TextValue("精度", 0));
list.Add(new TextValue("线性度", 1));
list.Add(new TextValue("一致性", 2));
for (int i = 0; i < list.Count; i++)
{
cbxPurpose.Items.Add(list[i]);
}
cbxPurpose.DisplayMember = "Text";
cbxPurpose.ValueMember = "Value";
}
public class ValueObj
{
public string Text { get; set; }
public string Value { get; set; }
public override string ToString()
{
return this.Text;
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
dynamic obj = comboBox1.SelectedItem;
MessageBox.Show(obj.Value);
}
/// <summary>
/// ComboBox的Item
/// </summary>
public class TextValue
{
public TextValue() { }
public TextValue(string inText, int inValue)
{
this.Text = inText;
this.Value = inValue;
}
private string _text;
private int _value;
/// <summary>
/// 文本
/// </summary>
public string Text
{
set { this._text = value; }
get { return this._text; }
}
/// <summary>
/// 值
/// </summary>
public int Value
{
set { this._value = value; }
get { return this._value; }
}
}
public partial class TempLateWave : Form
{
List<TextValue> PurposeList = new List<TextValue>();
public TempLateWave()
{
InitializeComponent();
PurposeList.Add(new TextValue("精度", 0));
PurposeList.Add(new TextValue("线性度", 1));
PurposeList.Add(new TextValue("一致性", 2));
cbx.DataSource = PurposeList; //cbx 就是ComboBox控件
cbx.DisplayMember = "Text";
cbx.ValueMember = "Value";
}
}