Exemplo n.º 1
0
        // Load options form
        private void btnMenu_Click(object sender, EventArgs e)
        {
            // Write data to file
            // string fileData = "";
            StreamWriter sw = new StreamWriter(Form1.frm1Ref.file);

            // Write number of locations to file
            sw.WriteLine(Form1.frm1Ref.numOfLocations);

            // For each location...
            foreach (var location in Form1.frm1Ref.locations)
            {
                // ...write it's location data to the file
                sw.WriteLine(location.GetName());
                sw.WriteLine(location.GetStreetName());
                sw.WriteLine(location.GetCountry());
                sw.WriteLine(location.GetPostCode());
                sw.WriteLine(location.GetLatitude());
                sw.WriteLine(location.GetLongitude());
                sw.WriteLine(location.GetNumOfYears());

                // For each year...
                foreach (var year in location.GetYears())
                {
                    // ...write it's description to the file
                    sw.WriteLine(year.GetDescription());

                    // For each month...
                    foreach (var month in year.GetMonthObs())
                    {
                        // ...write the year ID...
                        sw.WriteLine(year.GetYearID());

                        // ...and the month data to the file
                        sw.WriteLine(month.GetIDNum());
                        sw.WriteLine(month.GetMaxTemp());
                        sw.WriteLine(month.GetMinTemp());
                        sw.WriteLine(month.GetNumDaysFrost());
                        sw.WriteLine(month.GetMilRain());
                        sw.WriteLine(month.GetHoursSun());
                    }
                }
            }

            // Close the writer
            sw.Close();

            // Load options form
            OptionsForm f = new OptionsForm();

            f.Show();
            // Destroy this form without doing close function
            this.Dispose();
        }
        // 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();
        }