从与 DateTimePicker 中选择的日期时间相关的文件中读取某些信息
Read certain information from a file relating to datetime chosen in DateTimePicker
我有一个包含天气信息的文件,我正在阅读并将其放入结构中,然后将其添加到列表中。我有一个 dateTimePicker 控件,用户将使用它来 select 一月份的日期,当他们这样做时,我需要它打印到 listBox 控件我拥有的那一天的日期和天气信息存储在列表中。我已经将文件标记化并放入结构变量并将其添加到列表中。
当我通过 dateTimePicker 控件选择日期时,我最终一次获得了所有日期和信息。我相信是因为我不明白如何使用 dateTimePicker 控件,所以我无法弄清楚。
因此,当我选择 2018 年 1 月 1 日时,我需要它在 listBox 中引入当天的日期和降水、高温、低温等天气信息。我希望我已经把一切都解释清楚了,让你明白。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace Chapter9_Problem1_
{
public partial class Form1 : Form
{
// create new structure
struct Weather
{
public string date;
public string precipitation;
public string highTemp;
public string lowTemp;
}
// create new list
private List<Weather> weatherList = new List<Weather>();
// method to get info from file and store into list
private void fileData()
{
StreamReader inputFile;
inputFile = File.OpenText("weather.txt");
while(!inputFile.EndOfStream)
{
// create instance of the weather structure
Weather weather = new Weather();
// read the file and store into info variable
string info = inputFile.ReadLine();
// create char array for delimiter
char[] delimiter = { ';' };
// split info by each delimiter
string[] tokens = info.Split(delimiter);
// store the values into the structure weather variables
weather.date = tokens[0];
weather.precipitation = tokens[1];
weather.highTemp = tokens[2];
weather.lowTemp = tokens[3];
// add to the list
weatherList.Add(weather);
}
}
public Form1()
{
InitializeComponent();
}
// dateTimePicker method
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
// call file data method
fileData();
// create datetime object
DateTime dateTime = new DateTime();
// assign datetime object to the datetimepicker control
dateTime = dateTimePicker1.Value;
foreach(Weather weather in weatherList)
{
listBox1.Items.Add(weather.date + weather.date + weather.highTemp + weather.lowTemp);
}
}
}
}
我会更改您的模型以使用 DateTime
而不是 string
作为日期:
struct Weather
{
public DateTime date;
public string precipitation;
public string highTemp;
public string lowTemp;
}
像这样更改您的阅读代码:
string[] tokens = info.Split(delimiter);
// try to parse the date
if (!DateTime.TryParseExact(tokens[0], "M/d/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out var date))
{
MessageBox.Show($"The file contains an invalid date: tokens[0].");
return;
}
// store the values into the structure weather variables
weather.date = date;
weather.precipitation = tokens[1];
weather.highTemp = tokens[2];
weather.lowTemp = tokens[3];
// add to the list
weatherList.Add(weather);
最后,将 dateTimePicker1_ValueChanged 中的代码更改为如下所示:
foreach(Weather weather in weatherList.Where(w => w.date.Date == dateTime.Date))
{
string dateAsString = weather.date.ToString("M/d/yyyy", CultureInfo.InvariantCulture);
listBox1.Items.Add(dateAsString + weather.highTemp + weather.lowTemp);
}
Documentation for TryParseExact:
Converts the specified string representation of a date and time to its DateTime equivalent using the specified format, culture-specific format information, and style. The format of the string representation must match the specified format exactly. The method returns a value that indicates whether the conversion succeeded.
Documentation for DateTime's ToString method:
Converts the value of the current DateTime object to its equivalent string representation using the specified format and culture-specific format information.
我还建议阅读 Microsoft 的 C# coding conventions 并在一定程度上遵循它们,因为遵循共同的开发风格可以让其他人更轻松地阅读和维护您的代码。
P.S。请注意,您每次更改日期选择器值时都会调用 fileData()
,并且 fileData()
在填充列表之前不会清除列表。您最终会得到这样的重复值。我建议在填充之前清除列表,或者将对 fileData()
的调用移动到表单加载事件。
我有一个包含天气信息的文件,我正在阅读并将其放入结构中,然后将其添加到列表中。我有一个 dateTimePicker 控件,用户将使用它来 select 一月份的日期,当他们这样做时,我需要它打印到 listBox 控件我拥有的那一天的日期和天气信息存储在列表中。我已经将文件标记化并放入结构变量并将其添加到列表中。
当我通过 dateTimePicker 控件选择日期时,我最终一次获得了所有日期和信息。我相信是因为我不明白如何使用 dateTimePicker 控件,所以我无法弄清楚。
因此,当我选择 2018 年 1 月 1 日时,我需要它在 listBox 中引入当天的日期和降水、高温、低温等天气信息。我希望我已经把一切都解释清楚了,让你明白。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace Chapter9_Problem1_
{
public partial class Form1 : Form
{
// create new structure
struct Weather
{
public string date;
public string precipitation;
public string highTemp;
public string lowTemp;
}
// create new list
private List<Weather> weatherList = new List<Weather>();
// method to get info from file and store into list
private void fileData()
{
StreamReader inputFile;
inputFile = File.OpenText("weather.txt");
while(!inputFile.EndOfStream)
{
// create instance of the weather structure
Weather weather = new Weather();
// read the file and store into info variable
string info = inputFile.ReadLine();
// create char array for delimiter
char[] delimiter = { ';' };
// split info by each delimiter
string[] tokens = info.Split(delimiter);
// store the values into the structure weather variables
weather.date = tokens[0];
weather.precipitation = tokens[1];
weather.highTemp = tokens[2];
weather.lowTemp = tokens[3];
// add to the list
weatherList.Add(weather);
}
}
public Form1()
{
InitializeComponent();
}
// dateTimePicker method
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
// call file data method
fileData();
// create datetime object
DateTime dateTime = new DateTime();
// assign datetime object to the datetimepicker control
dateTime = dateTimePicker1.Value;
foreach(Weather weather in weatherList)
{
listBox1.Items.Add(weather.date + weather.date + weather.highTemp + weather.lowTemp);
}
}
}
}
我会更改您的模型以使用 DateTime
而不是 string
作为日期:
struct Weather
{
public DateTime date;
public string precipitation;
public string highTemp;
public string lowTemp;
}
像这样更改您的阅读代码:
string[] tokens = info.Split(delimiter);
// try to parse the date
if (!DateTime.TryParseExact(tokens[0], "M/d/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out var date))
{
MessageBox.Show($"The file contains an invalid date: tokens[0].");
return;
}
// store the values into the structure weather variables
weather.date = date;
weather.precipitation = tokens[1];
weather.highTemp = tokens[2];
weather.lowTemp = tokens[3];
// add to the list
weatherList.Add(weather);
最后,将 dateTimePicker1_ValueChanged 中的代码更改为如下所示:
foreach(Weather weather in weatherList.Where(w => w.date.Date == dateTime.Date))
{
string dateAsString = weather.date.ToString("M/d/yyyy", CultureInfo.InvariantCulture);
listBox1.Items.Add(dateAsString + weather.highTemp + weather.lowTemp);
}
Documentation for TryParseExact:
Converts the specified string representation of a date and time to its DateTime equivalent using the specified format, culture-specific format information, and style. The format of the string representation must match the specified format exactly. The method returns a value that indicates whether the conversion succeeded.
Documentation for DateTime's ToString method:
Converts the value of the current DateTime object to its equivalent string representation using the specified format and culture-specific format information.
我还建议阅读 Microsoft 的 C# coding conventions 并在一定程度上遵循它们,因为遵循共同的开发风格可以让其他人更轻松地阅读和维护您的代码。
P.S。请注意,您每次更改日期选择器值时都会调用 fileData()
,并且 fileData()
在填充列表之前不会清除列表。您最终会得到这样的重复值。我建议在填充之前清除列表,或者将对 fileData()
的调用移动到表单加载事件。