winform技巧二,listbox绑定,value,text,
2008-05-25 13:49 Virus-BeautyCode 阅读(3190) 评论(0) 编辑 收藏 举报private void button1_Click(object sender, EventArgs e)
{
List<Vector> list = new List<Vector>();
list.Add(new Vector("swb1", 1));
list.Add(new Vector("swb2", 2));
list.Add(new Vector("swb3", 3));
list.Add(new Vector("swb4", 4));
list.Add(new Vector("swb5", 5));
listBox1.DataSource = list;
listBox1.DisplayMember = "Name";
listBox1.ValueMember = "Id";
this.textBox1.Text = "Id";
this.textBox2.Text = "Name";
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//this.textBox1.Text = listBox1.SelectedItem
object obj = listBox1.Items[this.listBox1.SelectedIndex];
this.textBox1.Text = ((Vector)obj).Name;
this.textBox2.Text = listBox1.SelectedValue.ToString(); ;
}
}
class Vector
{
private string _name;
private int _id;
public string Name
{
get { return this._name; }
set { this._name = value; }
}
public int Id
{
get { return this._id; }
set { this._id = value; }
}
public Vector()
{ }
public Vector(string name, int id)
{
this._name = name;
this._id = id;
}
}