c# - 循环读取 StreamReader 文件输入并呈现在表单标签中
问题描述
我的程序允许用户从 DateTimePicker 中选择一个日期(2018 年 1 月 1 日-2018 年 1 月 31 日)。Whenever a date is selected, the imported text file (weather.txt) from StreamReader should display the selected date in the dateResultLabel (which I have already done), as well as the precipitation, high temp and low temp for that day(file is按日期显示;降水量;高温;低温)。我无法找到一种方法来在其受人尊敬的标签中显示其他 3 个结果。我在想一个循环可以检查文本文件,看看日期是否匹配并显示正确的数字,但我不太确定如何处理这个问题。我用 ListBox 程序做过类似的事情,但这是我第一次用 DateTimePicker 做这件事。DisplayData 方法是我计划放置循环的地方。图像和代码如下。
public partial class Form1 : Form
{
private List<WeatherData> weatherList = new List<WeatherData>();
public Form1()
{
InitializeComponent();
}
private void ReadFile()
{
try
{
// Call StreamReader to use imported file
StreamReader inputFile;
string line;
WeatherData entry = new WeatherData();
// Delimiter to separate values in text file
char[] delim = { ';' };
// Open text file
inputFile = File.OpenText("weather.txt");
while (!inputFile.EndOfStream)
{
line = inputFile.ReadLine();
string[] tokens = line.Split(delim);
DateTime.TryParse(tokens[0], out entry.date);
DateTime.TryParse(tokens[1], out entry.precipitation);
DateTime.TryParse(tokens[2], out entry.hightemp);
DateTime.TryParse(tokens[3], out entry.lowtemp);
weatherList.Add(entry);
}
}
catch (Exception ex)
{
// Shows error message
MessageBox.Show(ex.Message);
}
}
private void DisplayData()
{
foreach (WeatherData entry in weatherList)
{
}
}
private void ExitButton_Click(object sender, EventArgs e)
{
// Closes the form
this.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
ReadFile();
DisplayData();
}
private void DateTimePicker_ValueChanged(object sender, EventArgs e)
{
timePicker.MinDate = DateTime.Today.AddDays(-767); // Limit minimum date to January 1st 2018
timePicker.MaxDate = DateTime.Now.AddDays(-737); // Limit maximum date to January 31st 2018
DateTime selected = timePicker.Value; // Stores selected date
dateResultLabel.Text = selected.ToString("d"); // Display date
}
}
解决方案
根据测试数据(POST 中提供的 txt 文件图像),降水、高温和低温似乎是整数数据类型。所以请将它们的数据类型更改为 int 以及解析逻辑以使用 int.TryParse。
修改WeatherData
结构如下:
struct WeatherData
{
public DateTime date;
public double precipitation;
public int hightemp;
public int lowtemp;
}
然后修改代码中的while循环,如下所示:
while (!inputFile.EndOfStream)
{
line = inputFile.ReadLine();
string[] tokens = line.Split(delim);
DateTime.TryParse(tokens[0], out entry.date);
double.TryParse(tokens[1], out entry.precipitation);
int.TryParse(tokens[2], out entry.hightemp);
int.TryParse(tokens[3], out entry.lowtemp);
weatherList.Add(entry);
}
在您的表单上有一个网格控件(或某种控件),然后修改您的显示数据以将天气列表绑定到网格。下面是一个将 weatherList 绑定到 DataGridView 的示例。您可以使用任何您的选择。
private void DisplayData()
{
// am assuming your grid view control name is dataGridView1. Otherwise, please replace with your control name.
// Initialize the DataGridView.
dataGridView1.AutoGenerateColumns = false;
dataGridView1.AutoSize = true;
// Initialize and add a text box column.
DataGridViewColumn column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "WeatherData.date";
column.Name = "Date";
dataGridView1.Columns.Add(column);
// same for other 3 columns - precipitation, hightemp and lowtemp
// Now bind data
datagridview.DataSource = weatherList;
}
请参考这个msdn 帖子。
推荐阅读
- javascript - 通过 jQuery 意外行为设置 id [我缺少什么?]
- r - 使用 apply 进行标准化
- typo3 - How to generate a rating meta tag in TYPO3 with caching?
- caching - postsharp - 基于参数值的缓存
- reactjs - 如何将 Ant Design 与 next.js 正确集成?链接无效
- ios - 如何更改 SwiftUI 中导航栏的背景颜色?
- html - 如何在 Shopify 中将 GiftShip 消息添加到装箱单模板?
- python - Python套接字将多个发送的数据放入一个接收缓冲区
- c# - 排序列表