private void ButtonAdd_Click(object sender, RoutedEventArgs e) { //MessageBox.Show("Hey, We are adding"); if (ButtonAdd.Content.Equals("Add")) { ButtonAdd.Style = (Style)FindResource("red"); ButtonAdd.Content = "Save"; //ButtonEdit.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#F6C8B8")); //ButtonEdit.Style = Resources["btnImportant"] as Style; TextBoxName.Text = ""; TextBoxAge.Text = ""; TextBoxName.IsReadOnly = false; TextBoxAge.IsReadOnly = false; ButtonEdit.IsEnabled = false; ButtonDelete.IsEnabled = false; } else { ButtonAdd.Content = "Add"; ButtonAdd.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#CFE8DC")); ButtonEdit.Style = (Style)FindResource("general"); //ButtonEdit.Style = Resources["btnRegular"] as Style; TextBoxName.IsReadOnly = true; TextBoxAge.IsReadOnly = true; if ((TextBoxAge.Text.Length > 0) && (TextBoxName.Text.Length > 0)) { if (rabbit != null) { rabbit.Name = TextBoxName.Text; if (int.TryParse(TextBoxAge.Text, out int age)) { rabbit.Age = age; } using (var db = new RabbitDbEntities()) { Rabbit rabbitToAdd = new Rabbit { Name = rabbit.Name, Age = rabbit.Age }; db.Rabbits.Add(rabbitToAdd); db.SaveChanges(); ListBoxRabbits.ItemsSource = null; ListBoxRabbits.Items.Clear(); rabbits = db.Rabbits.ToList(); ListBoxRabbits.ItemsSource = rabbits; } } } } }
private void ButtonDelete_Click(object sender, RoutedEventArgs e) { if (ButtonDelete.Content.Equals("Delete")) { ButtonDelete.Content = "Confirm Delete"; } else { // delete record // find record in database which matches selected rabbit if (rabbit != null) { using (var db = new RabbitDbEntities()) { var rabbitToDelete = db.Rabbits.Find(rabbit.RabbitId); db.Rabbits.Remove(rabbitToDelete); db.SaveChanges(); //ListBoxRabbits.ItemsSource = null; rabbits = db.Rabbits.ToList(); ListBoxRabbits.ItemsSource = rabbits; } } TextBoxName.Text = ""; TextBoxAge.Text = ""; ButtonDelete.IsEnabled = false; ButtonDelete.Content = "Delete"; } }
private void ButtonDelete_Click(object sender, RoutedEventArgs e) { if (ButtonDelete.Content.Equals("Delete")) { EnterDeleteMode(); } else { EnterSelectingMode(); if (rabbit != null) { using (var db = new RabbitDbEntities()) { var rabbitToDelete = db.Rabbits.Find(rabbit.RabbitId); db.Rabbits.Remove(rabbitToDelete); db.SaveChanges(); //repopulate listbox ListBoxRabbits.ItemsSource = null; ListBoxRabbits.Items.Clear(); rabbits = db.Rabbits.ToList(); ListBoxRabbits.ItemsSource = rabbits; } } } // if ((TextBoxAge.Text.Length > 0) && (TextBoxName.Text.Length > 0)) }
private void ButtonAdd_Click(object sender, RoutedEventArgs e) { if (ButtonAdd.Content.Equals("Add")) { EnterAddSaveMode(); } else { EnterSelectingMode(); if ((TextBoxAge.Text.Length > 0) && (TextBoxName.Text.Length > 0)) { if (int.TryParse(TextBoxAge.Text, out int age)) { var RabbitToAdd = new Rabbit() { Name = TextBoxName.Text, Age = age }; //read db and add new rabbit using (var db = new RabbitDbEntities()) { db.Rabbits.Add(RabbitToAdd); db.SaveChanges(); rabbit = null; rabbits = db.Rabbits.ToList(); ListBoxRabbits.ItemsSource = rabbits; } } } } }
private void ButtonEdit_Click(object sender, RoutedEventArgs e) { if (ButtonEdit.Content.Equals("Edit")) { ButtonEdit.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#9589FF")); ButtonEdit.Content = "Save"; TextBoxName.IsReadOnly = false; TextBoxAge.IsReadOnly = false; TextBoxName.Background = (SolidColorBrush)Brushes.White; TextBoxAge.Background = (SolidColorBrush)Brushes.White; ButtonAdd.IsEnabled = false; TextBoxName.Focus(); TextBoxName.SelectAll(); } else { Color color = (Color)ColorConverter.ConvertFromString("#B3C6ED"); var brush = new SolidColorBrush(color); ButtonEdit.Background = brush; ButtonEdit.Content = "Edit"; if ((TextBoxAge.Text.Length > 0) && (TextBoxName.Text.Length > 0)) { // must have selected a rabbit if (rabbit != null) { rabbit.Name = TextBoxName.Text; if (int.TryParse(TextBoxAge.Text, out int age)) { rabbit.Age = age; } using (var db = new RabbitDbEntities()) { // read rabbit from database by ID var rabbitToUpdate = db.Rabbits.Find(rabbit.RabbitId); // update rabbit rabbitToUpdate.Name = rabbit.Name; rabbitToUpdate.Age = rabbit.Age; // save rabbit back to DB db.SaveChanges(); // clear listbox because we're going to change (the binding) rabbit = null; // remove binding on rabbit // ListBoxRabbits.ItemsSource = null; // remove binding // ListBoxRabbits.Items.Clear(); // clear it out // repopulate listbox // re-read from db rabbits = db.Rabbits.ToList(); // get rabbits ListBoxRabbits.ItemsSource = rabbits; // bind to listbox again } } } ButtonAdd.IsEnabled = true; } }
private void ButtonAdd_Click(object sender, EventArgs e) { if (ButtonAdd.Content.Equals("Add")) { ButtonAdd.Content = "Save"; ButtonAdd.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#9589FF")); // clear boxes and set to white TextBoxName.Text = ""; TextBoxAge.Text = ""; TextBoxName.Background = (SolidColorBrush)Brushes.White; TextBoxAge.Background = (SolidColorBrush)Brushes.White; TextBoxName.IsReadOnly = false; TextBoxAge.IsReadOnly = false; ButtonEdit.IsEnabled = false; ButtonDelete.IsEnabled = false; } else { ButtonAdd.Content = "Add"; ButtonAdd.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#B3C6ED")); TextBoxName.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#D3E9ED")); TextBoxAge.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#D3E9ED")); TextBoxName.IsReadOnly = true; TextBoxAge.IsReadOnly = true; // commit changes if ((TextBoxName.Text.Length > 0) && (TextBoxAge.Text.Length > 0)) { // get age if (int.TryParse(TextBoxAge.Text, out int age)) { var RabbitToAdd = new Rabbit() { Name = TextBoxName.Text, Age = age }; // read db and add new rabbit using (var db = new RabbitDbEntities()) { db.Rabbits.Add(RabbitToAdd); db.SaveChanges(); // update view rabbit = null; rabbits = db.Rabbits.ToList(); ListBoxRabbits.ItemsSource = null; ListBoxRabbits.ItemsSource = rabbits; } } } } }
private void ButtonDelete_Click(object sender, RoutedEventArgs e) { //MessageBox.Show("Hey, We are deleting"); if (ButtonDelete.Content.Equals("Delete")) { ButtonDelete.Style = (Style)FindResource("red"); ButtonDelete.Content = "Confirm Delete"; //ButtonEdit.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#F6C8B8")); //ButtonEdit.Style = Resources["btnImportant"] as Style; ButtonAdd.IsEnabled = false; } else { ButtonDelete.Content = "Delete"; ButtonDelete.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#CFE8DC")); ButtonEdit.Style = (Style)FindResource("general"); //ButtonEdit.Style = Resources["btnRegular"] as Style; using (var db = new RabbitDbEntities()) { if (rabbit != null) { //var rtodel = db.Rabbits.Find(rabbit.RabbitId); //db.Rabbits.Remove(rtodel); db.Rabbits.Remove(db.Rabbits.Single(a => a.RabbitId == rabbit.RabbitId)); //db.Rabbits.Remove(rabbit); db.SaveChanges(); TextBoxName.Text = ""; TextBoxAge.Text = ""; ListBoxRabbits.ItemsSource = null; ListBoxRabbits.Items.Clear(); rabbits = db.Rabbits.ToList(); ListBoxRabbits.ItemsSource = rabbits; } } ButtonDelete.IsEnabled = false; ButtonAdd.IsEnabled = true; } }
private void ButtonEdit_Click(object sender, RoutedEventArgs e) { if (ButtonEdit.Content.Equals("Edit")) { EnterEditSaveMode(); } else { EnterSelectingMode(); if ((TextBoxAge.Text.Length > 0) && (TextBoxName.Text.Length > 0)) { if (rabbit != null) { rabbit.Name = TextBoxName.Text; if (int.TryParse(TextBoxAge.Text, out int age)) { rabbit.Age = age; } using (var db = new RabbitDbEntities()) { //read rabbit from database by ID var rabbitToUpdate = db.Rabbits.Find(rabbit.RabbitId); //update rabbit rabbitToUpdate.Name = rabbit.Name; rabbitToUpdate.Age = rabbit.Age; //save rabbit back to DB db.SaveChanges(); //repopulate listbox UpdateRabbitsListBox(db.Rabbits.ToList()); } } } } }