对头,你可以定义一个数据类,然后做为Listbox的项目 注意重写ToString方法 定义数据类 public class ListBoxItemData { public string Name { get; set; } public int Age { get; set; } public String Address { get; set; } public override string ToString() { return this.Name; } }使用方法 private void button1_Click( object sender, EventArgs e ) { ListBox listBox = new ListBox(); listBox.Items.Add(new ListBoxItemData(){ Name="AAA",Age=13,Address="长江"}); listBox.Items.Add( new ListBoxItemData() { Name = "BBB", Age = 23, Address = "黄河" } ); listBox.Items.Add( new ListBoxItemData() { Name = "CCC", Age = 44, Address = "黄山" } ); //取数据 ListBoxItemData data = listBox.SelectedItem as ListBoxItemData; MessageBox.Show( data.Name ); }
public override string ToString() { return this.Name; }表示在ListBox中显示的内容
注意重写ToString方法
定义数据类 public class ListBoxItemData
{
public string Name
{
get;
set;
} public int Age
{
get;
set;
} public String Address
{
get;
set;
} public override string ToString()
{
return this.Name;
}
}使用方法 private void button1_Click( object sender, EventArgs e )
{
ListBox listBox = new ListBox();
listBox.Items.Add(new ListBoxItemData(){ Name="AAA",Age=13,Address="长江"});
listBox.Items.Add( new ListBoxItemData()
{
Name = "BBB",
Age = 23,
Address = "黄河"
} );
listBox.Items.Add( new ListBoxItemData()
{
Name = "CCC",
Age = 44,
Address = "黄山"
} ); //取数据
ListBoxItemData data = listBox.SelectedItem as ListBoxItemData;
MessageBox.Show( data.Name );
}
{
return this.Name;
}表示在ListBox中显示的内容