如何(或者我可以)列出通用列表中对象的属性,并将其显示在组合框中

发布于 2024-11-17 05:36:02 字数 1633 浏览 5 评论 0原文

我只是一个初学者,所以我想我会经常遇到这样的问题。

事情是这样的。

我有一个通用的对象列表,比如说车库,许多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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浪荡不羁 2024-11-24 05:36:02

我不知道您使用哪个组合(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

恍梦境° 2024-11-24 05:36:02

您可以拥有以下

    myComboBox.DisplayMember = "year";//Here year is the car year
    myComboBox.ValueMember = "name";//Here name is the car name
    myComboBox.DataSource = garage;

谢谢
阿什瓦尼

You can have following

    myComboBox.DisplayMember = "year";//Here year is the car year
    myComboBox.ValueMember = "name";//Here name is the car name
    myComboBox.DataSource = garage;

Thanks
Ashwani

别想她 2024-11-24 05:36:02

现在我有一个解决方案,就是使用迭代器来添加项目。

foreach (car c in garage)
        {
            cBox1.Items.Add(c.make);
            cBox2.Items.Add(c.year);
        }

这已经足够好了。

Now I have a solution, is to use iterator to add items.

foreach (car c in garage)
        {
            cBox1.Items.Add(c.make);
            cBox2.Items.Add(c.year);
        }

this will work good enough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文