Example #1
0
        private void ButtonOkClick(object sender, EventArgs e)
        {
            // take focus away from time pickers
            buttonOK.Focus();

            // get start and end times from input
            var start = TimeOfDay.FromDateTime(dateTimePickerStart.Value);
            var end   = TimeOfDay.FromDateTime(dateTimePickerEnd.Value);

            // if it starts before it ends
            if (start >= end)
            {
                MessageBox.Show("Start time must be before end time.", "Unavailable Timeslot", MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation);
                return;
            }

            // if it's not within the scope of the timetable
            if (end <= new TimeOfDay(_earliest, 0))
            {
                MessageBox.Show("End time must be after " + _earliest + "am.", "Unavailable Timeslot", MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation);
                dateTimePickerEnd.Focus();
                return;
            }
            if (start >= new TimeOfDay(_latest, 0))
            {
                MessageBox.Show("Start time must be before " + _latest + "pm.", "Unavailable Timeslot", MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation);
                dateTimePickerStart.Focus();
                return;
            }

            // clip the timeslot to be within the bounds of the timetable
            if (start < new TimeOfDay(_earliest, 0))
            {
                start = new TimeOfDay(_earliest, 0);
            }
            if (end > new TimeOfDay(_latest, 0))
            {
                end = new TimeOfDay(_latest, 0);
            }

            // build new unavailability from input
            var unavail = new Unavailability(textBoxName.Text, comboBoxDay.SelectedIndex,
                                             -1, start.Hour, start.Minute, end.Hour, end.Minute);

            if (_unavail != null && _unavail.EquivalentTo(unavail) && _unavail.Name == unavail.Name)
            {
                DialogResult = DialogResult.Cancel;
                Close();
                return;
            }

            // if editing, remove the old timeslot
            if (_unavail != null)
            {
                _timetable.UnavailableList.Remove(_unavail);
            }

            // if it doesn't fit in the current timetable
            if (!_timetable.FreeDuring(unavail, true))
            {
                MessageBox.Show("Selected time slot is currently occupied.", "Unavailable Timeslot",
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                // if editing, add the old timeslot back in
                if (_unavail != null)
                {
                    _timetable.UnavailableList.Add(_unavail);
                }
                return;
            }

            // insert the edited timeslot
            _timetable.UnavailableList.Add(unavail);

            // recompute solutions
            _timetable.RecomputeSolutions = true;
            // return
            DialogResult = DialogResult.OK;
            Close();
        }