简体   繁体   中英

Reading in a file and creating text boxes dynamically

namespace A3_Reese
{

    public partial class Form1 : Form
    {

creating the car class

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

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

Creating a dynamic labels and textboxes using a for loop

            // 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");
        }

Loading in the car class using a method I created called 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)
            {

//spliting the text file by spaces using an array to split up the pieces in the for loop by ' ' and making them equal to a variable defined in the 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();
        }
    }

Creating the a class called CarsInfo to store values I will need internal class CarsInfo { private string carMake; private string carModel; private int year; private double mileage;

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

        }


    }
}

If you want to traverse the data in the file to the Form, you can use the for statement. And get the corresponding value based on the index. Besides, to access the items in list "cars", you need to define some 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; } }
}

Now, assuming you have finished file reading, you can refer to the code below to create controls dynamically.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM