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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-27
  • 2021-12-23
  • 2022-03-05
  • 2021-08-26
猜你喜欢
  • 2021-09-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案