private struct selNameValue { public string sName; public string sValue; public selNameValue(string sParaName, string sParaValue) { this.sName = sParaName; this.sValue = sParaValue; } public string Name { get { return this.sName; } } public string Value { get { return this.sValue; } } public override string ToString() { return this.Name + " - " + this.Value; } }//构造函数中 // Populates the list box using DataSource. // DisplayMember is used to display just the long name of each state. ArrayList ArrSrc = new ArrayList(); ArrSrc.Add(new selNameValue("Text1", "1"));//此处传值name,code ArrSrc.Add(new selNameValue("Text2", "2")) ; ArrSrc.Add(new selNameValue("Text3", "3")); this.comboBox1.DataSource = ArrSrc; this.comboBox1.DisplayMember = "Name";//selNameValue结构中的属性 this.comboBox1.ValueMember = "Value"; //selNameValue结构中的属性private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e) { if (this.comboBox1.SelectedIndex != -1) MessageBox.Show(this.comboBox1.SelectedValue.ToString()); }
{
public string sName;
public string sValue;
public selNameValue(string sParaName, string sParaValue)
{
this.sName = sParaName;
this.sValue = sParaValue;
}
public string Name
{
get
{
return this.sName;
}
}
public string Value
{
get
{
return this.sValue;
}
}
public override string ToString()
{
return this.Name + " - " + this.Value;
}
}//构造函数中
// Populates the list box using DataSource.
// DisplayMember is used to display just the long name of each state.
ArrayList ArrSrc = new ArrayList();
ArrSrc.Add(new selNameValue("Text1", "1"));//此处传值name,code
ArrSrc.Add(new selNameValue("Text2", "2")) ;
ArrSrc.Add(new selNameValue("Text3", "3"));
this.comboBox1.DataSource = ArrSrc;
this.comboBox1.DisplayMember = "Name";//selNameValue结构中的属性
this.comboBox1.ValueMember = "Value"; //selNameValue结构中的属性private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (this.comboBox1.SelectedIndex != -1)
MessageBox.Show(this.comboBox1.SelectedValue.ToString());
}