首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在C# WFA中将列表中的类实例属性添加到组合框项目

在C# WFA中将列表中的类实例属性添加到组合框项目
EN

Stack Overflow用户
提问于 2018-05-11 05:01:30
回答 1查看 44关注 0票数 0

我在一个文件中有一个茶的数据库,我从文件中为数据库中的每个茶创建了一个新的Tea类实例,并将每个tea类型的对象放在一个List<Tea>中。

现在,我需要在ComboBox中传递茶的名称,但我可能无法使用foreach,对吗?但是怎么做呢?请帮帮我!我可能对这个类做错了什么,请看我的类代码(它是捷克语的,不要紧张):

代码语言:javascript
复制
class Caj
{

    public static string Nazev
    {
        get;
        set;
    }
    public static string ZemePuvodu
    {
        get;
        set;
    }
    public static string Detail
    {
        get;
        set;
    }
    public static string BarvaNalevu
    {
        get;
        set;
    }
    public static string Chut
    {
        get;
        set;
    }

    public static int Cena
    {
        get;
        set;
    }

    public static int DobaLouhovani
    {
        get;
        set;
    }

    public static int PocetLzicek
    {
        get;
        set;
    }

    public static string Nalevy
    {
        get;
        set;
    }

    public static int TeplotaVody
    {
        get;
        set;
    }
    public Caj(string nazev, string zemePuvodu, string detail, string barvaNalevu, string chut, int cena, int dobaLouhovani, int pocetLzicek, string nalevy, int teplotaVody)
    {
        Nazev = nazev;
        ZemePuvodu = zemePuvodu;
        Detail = detail;
        BarvaNalevu = barvaNalevu;
        Chut = chut;
        Cena = cena;
        DobaLouhovani = dobaLouhovani;
        PocetLzicek = pocetLzicek;
        Nalevy = nalevy;
        TeplotaVody = teplotaVody;
    }
}  

这是我现在的表单代码:

代码语言:javascript
复制
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace Zika_projekt
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        static string cesta = "databaze.txt";
        static int pocetCaju = File.ReadAllLines(cesta).Length;

        private static List<Caj> databaze = new List<Caj>();


        private void Form1_Load(object sender, EventArgs e)
        {

            if (File.Exists(cesta))
            {
                FileStream fs = new FileStream(cesta, FileMode.OpenOrCreate);
                StreamReader sr = new StreamReader(fs, Encoding.UTF8);
                cbNazevCaje.Items.Clear();
                for (int i = 0; i < pocetCaju; i++)
                {
                    for (int j = 1; j < i;)
                    {
                        sr.ReadLine();
                    }
                    string[] caj = sr.ReadLine().Split(';');
                    Caj novyCaj = new Caj(caj[0], caj[1], caj[2], caj[3], caj[4], Convert.ToInt32(caj[5]), Convert.ToInt32(caj[6]), Convert.ToInt32(caj[7]), caj[8], Convert.ToInt32(caj[9]));
                    cbNazevCaje.Items.Add(novyCaj.);
                    databaze.Add(novyCaj);
                }
                sr.Close();
                fs.Close();
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2018-05-11 05:22:44

您可以使用组合框的DataSource属性:

代码语言:javascript
复制
cbNazevCaje.DataSource = databaze;
cbNazevCaje.DisplayMember = "Nazev";  // I'm guessing Nazev is Czech for Name?
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50281445

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档