private void openToolStripMenuItem_Click(object sender, EventArgs e) { if (uxOpenFile.ShowDialog() == DialogResult.OK) { try //try to read the input file { //read in the file and put it into the listbox WeatherList temp = new WeatherList(); char[] delims = { ' ' }; using (StreamReader sr = new StreamReader(uxOpenFile.FileName)) { while (!sr.EndOfStream) { string[] pieces = (sr.ReadLine()).Split(delims, StringSplitOptions.RemoveEmptyEntries); int year = Convert.ToInt32(pieces[2]); int month = Convert.ToInt32(pieces[0]); int day = Convert.ToInt32(pieces[1]); double temperature = Convert.ToDouble(pieces[3]); DateTime date = new DateTime(year, month, day); WeatherData weather = new WeatherData(date, temperature); temp.Add(weather); } } _current = temp; uxWeatherListBox.DataSource = _current; } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
/// <summary> /// on open menu click, loads file and listbox contents /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void uxOpenMenu_Click(object sender, EventArgs e) //More error checking here if time { if (uxOpenFileDialog.ShowDialog() == DialogResult.OK) { //try //{ if (uxDatesList.Items.Count > 0) { uxDatesList.DataSource = null; uxDatesList.Items.Clear(); wd = new WeatherList(); } string name = uxOpenFileDialog.FileName; using (StreamReader inFile = new StreamReader(name)) { string line; char[] sep = new char[] { ' ' }; while ((line = inFile.ReadLine()) != null) { string[] data = new string[4]; data = line.Split(sep, StringSplitOptions.RemoveEmptyEntries); if (Convert.ToDouble(data[3]) != -99) { wd.Add(new WeatherData(Convert.ToInt32(data[0]), Convert.ToInt32(data[1]), Convert.ToInt32(data[2]), Convert.ToDouble(data[3]))); } } } uxDatesList.DataSource = wd; //}catch(Exception ex) //{ // MessageBox.Show("Something Went Wrong\n" + ex.ToString()); //} } }