private void buttonSave_Click(object sender, EventArgs e)
        {
            // validation code
            bool validationFailed = false;

            if (textBoxSubject.Text == "") // the user didn't enter anything in the subject text box
            {
                textBoxSubject.BackColor = Color.Red;
                validationFailed         = true;
            }

            else if (textBoxLocation.Text == "") // the user didn't enter anything in the location text box
            {
                textBoxLocation.BackColor = Color.Red;
                validationFailed          = true;
            }

            if (validationFailed) // the validation failed
            {
                // 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);
                string displayText     = textBoxSubject.Text + "," + textBoxLocation.Text;
                finalData = Convert.ToString(1) + '\t' + start + '\t' + length + '\t' + displayText;
                SingleCalendarEvent singleEvent = new SingleCalendarEvent(finalData);
                // setting the onject values to the values that are in the single calendar event class
                _SingleEvent = singleEvent;
                DialogResult = DialogResult.OK; // close  the dialog box when the save is completed
            }
        }
Esempio n. 2
0
        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);
        }