// Save year to array private void SaveNewYear() { Boolean duplicateFound = false; // Pointer Variable int curYearCheck = -1; // If there are no years in array, skip duplicate checking if (Form1.frm1Ref.locations[curLocation].GetNumOfYears() != 0) { // Loop while there are still years left unchecked do { curYearCheck += 1; // If the entered year id matches an exsisting id, it is a duplicate if (Form1.frm1Ref.locations[curLocation].GetYears()[curYearCheck].GetYearID() == Convert.ToInt64(txtBxYearID.Text)) { duplicateFound = true; } } while (curYearCheck != Form1.frm1Ref.locations[curLocation].GetNumOfYears() - 1); } // If there are duplicates... if (duplicateFound) { // ...show error message MessageBox.Show("There is already a year with this ID."); } // If there are no duplicates... else { Form1.frm1Ref.locations[curLocation].SetNumOfYears(Form1.frm1Ref.locations[curLocation].GetNumOfYears() + 1); // ...save year to array Year thisYear = new Year( txtBxDescription.Text, Convert.ToInt32(txtBxYearID.Text) ); Form1.frm1Ref.locations[curLocation].SetYears(thisYear, Form1.frm1Ref.locations[curLocation].GetNumOfYears() - 1); // Create a tempary monthly observation MonthlyObservation tempMonth = new MonthlyObservation( 0, 0, 0, 0, 0, 0 ); // Add 12 tempary monthly observations to the array for (int i = 0; i < 12; i++) { Form1.frm1Ref.locations[curLocation].GetYears()[Form1.frm1Ref.locations[curLocation].GetNumOfYears() - 1].SetMonthObs(tempMonth, i); } } }
// Save month to the array private void SaveNewMonth() { Boolean duplicateFound = false; // Pointer Variable int curMonthCheck = -1; // Loop while there are still months left unchecked do { curMonthCheck += 1; // If the month has no data, skip duplicate check if (Form1.frm1Ref.locations[curLocation].GetYears()[curYear].GetMonthObs()[curMonthCheck] != null) { // If the entered month id matches an exsisting id, it is a duplicate if (Form1.frm1Ref.locations[curLocation].GetYears()[curYear].GetMonthObs()[curMonthCheck].GetIDNum() == Convert.ToInt64(cmbxMonthID.Text)) { duplicateFound = true; } } } while (curMonthCheck != 11); // If there is a duplicate... if (duplicateFound) { // ...show error message MessageBox.Show("There is already a month with this ID."); } // If there is no duplicates... else { // ...create a month... MonthlyObservation thisMonth = new MonthlyObservation( Convert.ToInt32(cmbxMonthID.Text), Convert.ToDouble(txtBxMaxTemp.Text), Convert.ToDouble(txtBxMinTemp.Text), Convert.ToInt32(txtBxNumDaysFrost.Text), Convert.ToDouble(txtBxMilRain.Text), Convert.ToDouble(txtBxHoursSun.Text) ); // ...and add it to the array Form1.frm1Ref.locations[curLocation].GetYears()[curYear].SetMonthObs(thisMonth, Convert.ToInt32(cmbxMonthID.Text) - 1); } }
// Set a single monthly obsevation public void SetMonthObs(MonthlyObservation monthIn, int currentMonth) { monthObs[currentMonth] = monthIn; }
// Add data from a file private void btnAddFile_Click(object sender, EventArgs e) { // Creates a file dialog OpenFileDialog fileDialog = new OpenFileDialog(); // Gives the file dialog a filter so only text files show fileDialog.Filter = "Text Files|*.txt"; // Shows the dialog fileDialog.ShowDialog(); string file = fileDialog.FileName; // Create stream reader using the file selected StreamReader sr = new StreamReader(file); // Find how many additional locations there are int numOfLocationsToAdd = Convert.ToInt32(sr.ReadLine()); // Find how many original locations there are int currentTotalLocations = Form1.frm1Ref.numOfLocations; // For each location to add for (int i = 0; i < numOfLocationsToAdd; i++) { // Resize the array Array.Resize(ref Form1.frm1Ref.locations, (currentTotalLocations + i + 1)); // Create the location Form1.frm1Ref.locations[currentTotalLocations + i] = new Location( sr.ReadLine(), sr.ReadLine(), sr.ReadLine(), sr.ReadLine(), sr.ReadLine(), sr.ReadLine(), Convert.ToInt32(sr.ReadLine()) ); // For each year in location for (int y = 0; y < Form1.frm1Ref.locations[currentTotalLocations + i].GetNumOfYears(); y++) { // Create a year Year thisYear = new Year( sr.ReadLine(), Convert.ToInt32(sr.ReadLine()) ); // Add year to location Form1.frm1Ref.locations[currentTotalLocations + i].SetYears(thisYear, y); // For each month in year for (int z = 0; z < 12; z++) { // Reads the repeated year id if (z != 0) { sr.ReadLine(); } // Create a month MonthlyObservation thisMonth = new MonthlyObservation( Convert.ToInt32(sr.ReadLine()), Convert.ToDouble(sr.ReadLine()), Convert.ToDouble(sr.ReadLine()), Convert.ToInt32(sr.ReadLine()), Convert.ToDouble(sr.ReadLine()), Convert.ToDouble(sr.ReadLine()) ); // Add month to year Form1.frm1Ref.locations[currentTotalLocations + i].GetYears()[y].SetMonthObs(thisMonth, z); } } } // Message box to confirm completion MessageBox.Show("Everything has been read in."); // Update number of locations Form1.frm1Ref.numOfLocations += numOfLocationsToAdd; }
// On button click // Using one function to prevent parsing issues with streamreader private void button1_Click(object sender, EventArgs e) { // Creates a file dialog OpenFileDialog fileDialog = new OpenFileDialog(); // Gives the file dialog a filter so only text files show fileDialog.Filter = "Text Files|*.txt"; // Shows the dialog fileDialog.ShowDialog(); file = fileDialog.FileName; // Create stream reader using the file selected StreamReader sr = new StreamReader(file); // Find how many locations there are numOfLocations = Convert.ToInt32(sr.ReadLine()); // For each location for (int i = 0; i < numOfLocations; i++) { // Resize the array Array.Resize(ref locations, (i + 1)); // Create the location locations[i] = new Location( sr.ReadLine(), sr.ReadLine(), sr.ReadLine(), sr.ReadLine(), sr.ReadLine(), sr.ReadLine(), Convert.ToInt32(sr.ReadLine()) ); // For each year in location for (int y = 0; y < locations[i].GetNumOfYears(); y++) { // Create a year Year thisYear = new Year( sr.ReadLine(), Convert.ToInt32(sr.ReadLine()) ); // Add year to location locations[i].SetYears(thisYear, y); // For each month in year for (int z = 0; z < 12; z++) { // Reads the repeated year id if (z != 0) { sr.ReadLine(); } // Create a month MonthlyObservation thisMonth = new MonthlyObservation( Convert.ToInt32(sr.ReadLine()), Convert.ToDouble(sr.ReadLine()), Convert.ToDouble(sr.ReadLine()), Convert.ToInt32(sr.ReadLine()), Convert.ToDouble(sr.ReadLine()), Convert.ToDouble(sr.ReadLine()) ); // Add month to year locations[i].GetYears()[y].SetMonthObs(thisMonth, z); } } } // Close file sr.Close(); // Load options form OptionsForm f = new OptionsForm(); f.Show(); this.Hide(); }