Beispiel #1
0
        private void ButtonDelete_Click(object sender, RoutedEventArgs e)
        {
            if (ButtonDelete.Content.ToString() == "Delete")
            {
                ButtonDelete.Content = "Are You Sure?";  // set boxes to Delete
                var brush = new BrushConverter();
                TextBoxId.Background          = (Brush)brush.ConvertFrom("#e03636");
                TextBoxDescription.Background = (Brush)brush.ConvertFrom("#e03636");
                TextBoxCategoryId.Background  = (Brush)brush.ConvertFrom("#e03636");
            }
            else
            {
                using (var db = new TaskDBEntities())
                {
                    var deleteTask = db.Tasks.Find(task.TaskID);
                    db.Tasks.Remove(deleteTask);
                    db.SaveChanges();
                    ListBoxTasks.ItemsSource = null; // reset list box
                    tasks = db.Tasks.ToList();
                    ListBoxTasks.ItemsSource = tasks;
                };

                ButtonDelete.Content          = "Delete";
                ButtonDelete.IsEnabled        = false;
                TextBoxDescription.IsReadOnly = true;
                TextBoxCategoryId.IsReadOnly  = true;
                var brush = new BrushConverter();
                TextBoxId.Background          = (Brush)brush.ConvertFrom("BlanchedAlmond");
                TextBoxDescription.Background = (Brush)brush.ConvertFrom("BlanchedAlmond");
                TextBoxCategoryId.Background  = (Brush)brush.ConvertFrom("BlanchedAlmond");
            }
        }
Beispiel #2
0
        private void ButtonAdd_Click(object sender, RoutedEventArgs e)
        {
            if (ButtonAdd.Content.ToString() == "Add")
            {
                ButtonAdd.Content = "Confirm";
                // set boxes to editable
                TextBoxDescription.Background = Brushes.White;
                TextBoxCategoryId.Background  = Brushes.White;
                TextBoxCategoryId.IsReadOnly  = false;
                TextBoxDescription.IsReadOnly = false;
                // clear out boxes
                TextBoxId.Text          = "";
                TextBoxDescription.Text = "";
                TextBoxCategoryId.Text  = "";
            }
            else
            {
                ButtonAdd.Content = "Add";
                // set boxes back to read only
                TextBoxDescription.IsReadOnly = true;
                TextBoxCategoryId.IsReadOnly  = true;
                var brush = new BrushConverter();
                TextBoxDescription.Background = (Brush)brush.ConvertFrom("BlanchedAlmond");
                TextBoxCategoryId.Background  = (Brush)brush.ConvertFrom("BlanchedAlmond");
                // add record to database
                int.TryParse(TextBoxCategoryId.Text, out int categoriesId);

                var taskToAdd = new Task()
                {
                    Description = TextBoxDescription.Text,
                    CategoryID  = categoriesId
                };

                using (var db = new TaskDBEntities())
                {
                    db.Tasks.Add(taskToAdd);
                    //if (taskToAdd.CategoryID.GetType() == typeof(string))
                    //{
                    //    var newCategory = new Category
                    //    {
                    //        CategoryName = TextBoxCategoryId.Text
                    //    };
                    //    db.Categories.Add(newCategory);


                    //}
                    db.SaveChanges();
                    ListBoxTasks.ItemsSource = null; // reset list box
                    tasks = db.Tasks.ToList();
                    ListBoxTasks.ItemsSource = tasks;
                }
            }
        }
Beispiel #3
0
        private void ButtonEdit_Click(object sender, RoutedEventArgs e)
        {
            if (ButtonEdit.Content.ToString() == "Edit")  // one = is to assign but two == is to validate
            {
                TextBoxDescription.IsReadOnly = false;
                TextBoxCategoryId.IsReadOnly  = false;
                ButtonEdit.Content            = "Save";

                TextBoxDescription.Background = Brushes.White;
                TextBoxCategoryId.Background  = Brushes.White;
            }
            else
            {
                using (var db = new TaskDBEntities())
                {
                    var taskToEdit = db.Tasks.Find(task.TaskID);
                    // update description & categoryid
                    taskToEdit.Description = TextBoxDescription.Text;
                    // convering category id to integer using tryparse
                    // because the textbox containing text is of type integer regardless of the type categoryid being of a type int
                    int.TryParse(TextBoxCategoryId.Text, out int categoryid);
                    taskToEdit.CategoryID = categoryid;
                    if (task.CategoryID != null)
                    {
                        ComboBoxCategory.SelectedIndex = (int)taskToEdit.CategoryID - 1;
                    }
                    else
                    {
                        ComboBoxCategory.SelectedItem = null;
                    }

                    // save the changes of the box
                    db.SaveChanges();
                    // update list box
                    ListBoxTasks.ItemsSource = null; // reset list box
                    tasks = db.Tasks.ToList();
                    ListBoxTasks.ItemsSource = tasks;
                }
                ButtonEdit.Content            = "Edit";
                TextBoxDescription.IsReadOnly = true;
                TextBoxCategoryId.IsReadOnly  = true;
                ButtonEdit.IsEnabled          = false;
                var brush = new BrushConverter();

                TextBoxDescription.Background = (Brush)brush.ConvertFrom("BlanchedAlmond");
                TextBoxCategoryId.Background  = (Brush)brush.ConvertFrom("BlanchedAlmond");
            }
        }