如何(或者我可以)列出通用列表中对象的属性,并将其显示在组合框中
我只是一个初学者,所以我想我会经常遇到这样的问题。
事情是这样的。
我有一个通用的对象列表,比如说车库
,许多Car
对象都在车库里。
List<Cars> garage=new List<Car>();
car有属性,如car.make;汽车.型号;汽车.年份;
现在我有了这个车库
列表,并且已经将几辆车添加到该列表中。
我想要做的是使用 ComboBox 列出这些汽车的一个特定属性。
例如,我想让这个下拉列表显示汽车的年份(假设所有汽车都有不同的年份)。 到目前为止我能走的就是这样,但不知道如何走得更远。
myComboBox.DataSource = garage???
有人可以帮我指出一些事情吗?我知道它应该与通用列表有关,但我的书只使用了几页,并没有进一步深入。
namespace test
{
public partial class Form1 : Form
{
List<car> garage = new List<car>();
public Form1()
{
InitializeComponent();
car c1 = new car(98, "corolla", "toyota");
car c2 = new car(99, "camary", "toyota");
car c3 = new car(00, "eclipse", "misubishi");
garage.Add(c1);
garage.Add(c2);
garage.Add(c3);
foreach (car c in garage)
{
cBox.Items.Add(c.make);
}
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(garage[cBox.SelectedIndex].make);
}
}
class car
{
public int year;
public string make;
public string brand;
public car(int y, string m, string b)
{
year = y;
make = m;
brand = b;
}
}
}
I'm just a beginner, so I think I'll run into such problem very often.
Here is the thing.
I have a generic list of objects, let's say garage
, many Car
objects are in the garage.
List<Cars> garage=new List<Car>();
Car has attribute, like car.make; car.model; car.year;
Now I have this garage
list, and several car has been added to this list.
what I want to do is to using a ComboBox to list one specific attribute of these cars.
for example I want to have this dropdown list displaying the car's year (assume all cars have distinct year).
All I can go so far is like this, but don't know how to go further.
myComboBox.DataSource = garage???
Could anyone help me point out something? I know it should has something to do with Generic List, but the books I have use only couple pages on this and not go into it further.
namespace test
{
public partial class Form1 : Form
{
List<car> garage = new List<car>();
public Form1()
{
InitializeComponent();
car c1 = new car(98, "corolla", "toyota");
car c2 = new car(99, "camary", "toyota");
car c3 = new car(00, "eclipse", "misubishi");
garage.Add(c1);
garage.Add(c2);
garage.Add(c3);
foreach (car c in garage)
{
cBox.Items.Add(c.make);
}
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(garage[cBox.SelectedIndex].make);
}
}
class car
{
public int year;
public string make;
public string brand;
public car(int y, string m, string b)
{
year = y;
make = m;
brand = b;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道您使用哪个组合(Winforms,WebForms),但应该有 DisplayMember (哪个属性将用作组合中的文本)和 ValueMember(哪个属性将用作值)
http://windowsclient.net/blogs/faqs/archive/2006/07/12/what-are-the-displaymember-and-valuemember.aspx
I don't know which combo You use (Winforms, WebForms) but there should DisplayMember (what attribute will be used as text in combo) and ValueMember (which attribute will be used as value)
http://windowsclient.net/blogs/faqs/archive/2006/07/12/what-are-the-displaymember-and-valuemember.aspx
您可以拥有以下
谢谢
阿什瓦尼
You can have following
Thanks
Ashwani
现在我有一个解决方案,就是使用迭代器来添加项目。
这已经足够好了。
Now I have a solution, is to use iterator to add items.
this will work good enough.