关于ListBox中保存对象的解决,请高手指点
namespace ListBoxTest {
public partial class Form1 : Form
{
ArrayList al = new ArrayList();
public ArrayList GetArrayListItemAll()
{
al.Add(new ListBoxItemsAll("周润发", 40));
al.Add(new ListBoxItemsAll("张学友", 45));
al.Add(new ListBoxItemsAll("周杰伦", 30));
al.Add(new ListBoxItemsAll("巩俐", 40));
al.Add(new ListBoxItemsAll("张韶涵", 28));
return al;
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ArrayList listboxitems = GetArrayListItemAll();
this.listBox1.DataSource = listboxitems;
foreach (ListBoxItemsAll a in listboxitems)
{
this.listBox1.Items.Add(a.Name);
//此处如何记住对象项的ListBoxItemsAll对象?
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
public class ListBoxItemsAll
{
public ListBoxItemsAll(string name, int age)
{
this.name = name;
this.age = age;
}
private string name;
private int age;
public string Name
{
get { return name; }
set { name = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
}
这个程序是我刚看了视频写的,ListBox1里面添加的是类ListBoxItemsAll的name,但是我现在要点击ListBox1然后将对应ListBoxItemsAll的对象提取出来,怎么做?请高人指点,我是新手
ArrayList里面是一些ListBoxItemsAll的对象,但是ListBox1里只有改对象的一个属性值,如何将ListBox1的选项和对应的ListBoxItemsAll的对象绑定在一起呢?