【发布时间】:2020-01-16 18:22:48
【问题描述】:
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 的类来存储我需要的值 内部类 CarsInfo { 私有字符串 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;
}
}
}
【问题讨论】:
-
为什么要发布designer.cs文件?您应该向我们展示您为解决问题而编写的代码
-
听起来像是网格的工作。或用户控件。或网格。
-
嘿伙计们,我完全发布了我的代码的错误部分,对此我感到非常抱歉,感谢您帮助我更正它