li

ning
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

winform技巧,listbox绑定,value,text,

Posted on 2010-06-18 13:25  lining  阅读(3748)  评论(0编辑  收藏  举报

from->http://www.cnblogs.com/virusswb/articles/1206888.html

 

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;
    }
}