示例#1
0
        /// <summary>
        /// Adds a habit to the list when the add button is clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void habitsAddButton_Click(object sender, EventArgs e)
        {
            if (habitsTextBox.Text == "")
            {
                MessageBox.Show("Please enter a habit name");
                return;
            }
            Habit h = new Habit(habitsTextBox.Text);

            habitsBox.Items.Add(h);
            habitsComboBox.Items.Add(h);
            habitsTextBox.Text = "";
        }
示例#2
0
        private void Save()
        {
            FileStream      habitFS    = new FileStream("habits.dat", FileMode.Create);
            FileStream      todoFS     = new FileStream("todos.dat", FileMode.Create);
            FileStream      reminderFS = new FileStream("reminders.dat", FileMode.Create);
            FileStream      eventFS    = new FileStream("events.dat", FileMode.Create);
            BinaryFormatter formatter  = new BinaryFormatter();

            Habit[] habits = new Habit[habitsBox.Items.Count];
            habitsBox.Items.CopyTo(habits, 0);
            Todo[] todos = new Todo[todoBox.Items.Count];
            todoBox.Items.CopyTo(todos, 0);
            Reminder[] reminders = new Reminder[reminderCheckList.Items.Count];
            reminderCheckList.Items.CopyTo(reminders, 0);
            Event[] events = new Event[eventListComboBox.Items.Count];
            eventListComboBox.Items.CopyTo(events, 0);

            try
            {
                formatter.Serialize(habitFS, habits);
                formatter.Serialize(todoFS, todos);
                formatter.Serialize(reminderFS, reminders);
                formatter.Serialize(eventFS, events);
            }
            catch (Exception e)
            {
                MessageBox.Show("Failed to serialize: Reason: " + e.Message);
                throw;
            }
            finally
            {
                habitFS.Close();
                todoFS.Close();
                reminderFS.Close();
                eventFS.Close();
            }
        }