读取文件并动态创建文本框

Reading in a file and creating text boxes dynamically

namespace A3_Reese
{

    public partial class Form1 : Form
    {

创建汽车class

        private List<CarsInfo> cars;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // read in the file
            loadCars("A3Vehicles.txt");

使用 for 循环创建动态标签和文本框

            // create labels dynamically
            for(int i = 0; i < 10; i++)
            {
                Label CarMake = new Label();

                CarMake.AutoSize = true;
                CarMake.Location = new System.Drawing.Point(39, 34*i+52);
                CarMake.Name = "label1";
                CarMake.Size = new System.Drawing.Size(35, 13);
                CarMake.TabIndex = 3;
                CarMake.Text = "label1";

                this.Controls.Add(label1);       

                Label CarModel = new Label();

                CarModel.AutoSize = true;
                CarModel.Location = new System.Drawing.Point(118, 34*i+12);
                CarModel.Name = "label2";
                CarModel.Size = new System.Drawing.Size(35, 13);
                CarModel.TabIndex = 4;
                CarModel.Text = "label1";

                TextBox CarMileage = new TextBox();

                this.textBox1.Location = new System.Drawing.Point(212, 34);
                this.textBox1.Name = "textBox1";
                this.textBox1.Size = new System.Drawing.Size(100, 20);
                this.textBox1.TabIndex = 2;


            }

        }

        private void BtnAverageJeepMileage_Click(object sender, EventArgs e)
        {

        }

        private void BtnHighestMileage_Click(object sender, EventArgs e)
        {
            if()


            MessageBox.Show("Highest mileage");
        }

使用我创建的名为 loadCars

的方法在汽车中加载 class
        private void loadCars(String filename)
        {
            //creating a method to load in cars no matter the file.
            cars = new List<CarsInfo>();

            StreamReader FileIn = new StreamReader(filename);

            String inputLine = FileIn.ReadLine();


            while (inputLine != null)
            {

//按空格分割文本文件 使用数组通过 ' ' 拆分 for 循环中的片段,并使它们等于 CarsInfo class.

中定义的变量
                String[] carPieces = inputLine.Split(' ');

                int year = int.Parse(carPieces[0]);
                double mileage = double.Parse(carPieces[1]);
                String carMake = carPieces[2];
                String carModel = carPieces[3];


                cars.Add(new CarsInfo(carMake, carModel, year, mileage));

                inputLine = FileIn.ReadLine();

            }
            FileIn.Close();
        }
    }

创建一个名为 CarsInfo 的 class 来存储我需要的值 内部 class 汽车信息 { 私人串车制作; 私人串车模型; 私人整数年; 私人双倍里程;

        public CarsInfo(string carMake, string carModel, int year, double mileage)
        {
            this.carMake = carMake;
            this.carModel = carModel;
            this.year = year;
            this.mileage = mileage;

        }


    }
}

如果要将文件中的数据遍历到Form中,可以使用for语句。并根据索引得到对应的值。此外,要访问列表 "cars" 中的项目,您需要定义一些 properties.

class CarsInfo
{
    string carMake;
    string carModel;
    int year;
    double mileage;
    public string CarMake { get { return carMake; } }
    public string CarModel { get { return carModel; } }
    public int Year { get { return year; } }
    public double Mileage { get { return mileage; } }
}

现在,假设你已经完成了文件读取,你可以参考下面的代码动态创建控件。

List<CarsInfo> cars = new List<CarsInfo> { new CarsInfo("C1", "M1", 2, 1000.3), new CarsInfo("C2", "M2", 5, 7654.34), new CarsInfo("C3", "M3", 3, 3225) };
for (int i = 0; i < cars.Count; i++)
{
    Label CarMake = new Label();
    CarMake.AutoSize = true;
    CarMake.Location = new System.Drawing.Point(39, 34 * i + 12);
    CarMake.Name = "labelMake" + i;
    CarMake.Size = new System.Drawing.Size(35, 13);
    CarMake.Text = cars[i].CarMake;
    this.Controls.Add(CarMake);

    Label CarModel = new Label();
    CarModel.AutoSize = true;
    CarModel.Location = new System.Drawing.Point(118, 34 * i + 12);
    CarModel.Name = "labelModel" + i;
    CarModel.Size = new System.Drawing.Size(35, 13);
    CarModel.Text = cars[i].CarModel;
    this.Controls.Add(CarModel);

    TextBox CarMileage = new TextBox();
    CarMileage.Location = new System.Drawing.Point(212, 34 * i + 12);
    CarMileage.Name = "textBoxMileag" + i;
    CarMileage.Size = new System.Drawing.Size(100, 20);
    CarMileage.TabIndex = 2;
    CarMileage.Text = cars[i].Mileage.ToString();
    this.Controls.Add(CarMileage);
}