首页 > 解决方案 > 循环读取 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


    }
}

标签: c#.net

解决方案


根据测试数据(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 帖子。


推荐阅读