首页 > 解决方案 > 读入文件并动态创建文本框

问题描述

namespace A3_Reese
{

    public partial class Form1 : Form
    {

创建汽车类

        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 的方法加载汽车类

        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类中定义的变量。

                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 的类来存储值我将需要 internal class CarsInfo { private string carMake; 私串carModel;私人 int 年;私人双倍里程;

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

        }


    }
}

标签: c#dynamictextboxlabel

解决方案


如果想将文件中的数据遍历到Form中,可以使用for语句。并根据索引得到对应的值。此外,要访问“汽车”列表中的项目,您需要定义一些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);
}

推荐阅读