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

时间:2018-05-10 21:01:30

标签: c#

我在一个文件中有一个茶的数据库,我从文件中为数据库中的每个茶创建一个具有名称,价格,风味等属性的Tea类的新实例,并将每个Tea类型对象放入a List<Tea>
现在我需要在ComboBox中传输茶叶的名称而我无法弄明白,可能是foreach,我是对的吗?但是怎么样?请帮忙!我可能在课上做错了,看我的班级代码(它是捷克语,不要吓坏了):

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;
    }
}  

这是我现在的表格代码:

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();
            }
        }
    }
}

0 个答案:

没有答案