public bool Load(string calendarEntriesFile) { // TODO. Add your code to load the data from the file specified in // calendarEntriesFile here. You can edit the following two // lines if you wish. // Load the calendar enteries into the CalendarEntries collection from the specified file //returns a value of true if the load was successful, otherwise a value of false is returnd. bool status = true; // This displays the status of whetrher the file was opened or not - defaults to true. StreamReader calendarEntries = null; try { // Opens the calendar entry file specified in the parameter (C:\Users\<username>\AppData\Roaming\Calendar\CalendarApplication\1.0.0.0\appointments.txt) calendarEntries = new StreamReader(calendarEntriesFile); // Add any entreies located in the file into the CalenderEntries list // List to temporarily store the lines read by the streamreader List <string> lines = new List <string>(); using (calendarEntries) { string line; // read the file, adding it to the list while ((line = calendarEntries.ReadLine()) != null) { lines.Add(line); } } string firstNumber; // string value of the first number. used in the conversion process int singleOrRepeating; // Int value to determine whether the calendar entrey is a single event '0' or a repeating event '1' foreach (var calendarEntrey in lines) { firstNumber = Convert.ToString(calendarEntrey).Substring(0, 1); // getting the first character of the saved data in the file that determins whether it is a single or recurring event singleOrRepeating = int.Parse(firstNumber); if (singleOrRepeating == 1) { // the calendar event is a single event SingleCalendarEvent singleEvent = new SingleCalendarEvent(Convert.ToString(calendarEntrey).Remove(1, 0)); base.Add(singleEvent); } else if (singleOrRepeating == 2) { // the calendar event is a recurring event RecurringCalendarEvent repeatingEvent = new RecurringCalendarEvent(Convert.ToString(calendarEntrey).Remove(1, 0)); base.Add(repeatingEvent); } } status = true; // the file was successfully opened } catch { // If the file hasn't been opened, then the program sets the status as false status = false; } finally { // closes the calendar entries file if (calendarEntries != null) { calendarEntries.Close(); } } // returns the status of if the file has been opened or not. return(status); }
private void buttonSave_Click_1(object sender, EventArgs e) { // validation code int howManyTimes; bool validationFailed = false; if (textBoxSubject.Text == "") { textBoxSubject.BackColor = Color.Red; validationFailed = true; } else if (textBoxLocation.Text == "") { textBoxLocation.BackColor = Color.Red; validationFailed = true; } else if (!int.TryParse(textBoxHowManyTimes.Text, out howManyTimes) || howManyTimes < 0 || howManyTimes > 999) { textBoxHowManyTimes.BackColor = Color.Red; validationFailed = true; } if (validationFailed) // the validation failes { // Show a message box that tells the user hasn't entered a valid value. MessageBox.Show("Please enter a value in the boxes highligheted in red", "Validation Failed"); } else // the validation was a success { // Variables to hold the data entered. string finalData; string dateWithoutTime = date.Date.ToString().Substring(0, 10); string start = dateWithoutTime + ' ' + comboBoxStartTime.Text; string length = comboBoxLength.Text.Substring(0, 3); // switch statement to convert combo box frequency selected item to int //then convert that int to string and store in displayText (which will go to the file as well) // do other stuff to help with enum thingy int enumFrequencyNumber = 0; switch (comboBoxFrequency.SelectedItem.ToString()) { case "Daily": enumFrequencyNumber = 0; break; case "Weekly": enumFrequencyNumber = 1; break; case "Fornightly": enumFrequencyNumber = 2; break; case "Monthly": enumFrequencyNumber = 3; break; case "Yearly": enumFrequencyNumber = 4; break; default: break; } string displayText = textBoxSubject.Text + "," + textBoxLocation.Text + "," + enumFrequencyNumber.ToString() + "," + textBoxHowManyTimes.Text; //RecurringFrequency RecurringFrequency = (RecurringFrequency)int.Parse(comboBoxFrequency.SelectedItem.ToString()); finalData = Convert.ToString(2) + '\t' + start + '\t' + length + '\t' + displayText; RecurringCalendarEvent repeatingEvent = new RecurringCalendarEvent(finalData); // setting the onject values to the values that are in the single calendar event class _RepeatingEvent = repeatingEvent; DialogResult = DialogResult.OK; // closes the dialog box when the event is created } }