void Initialise() { //using -> has automatic cleanup (c# doesnt know when we're done so this helps it understand and clean all memory) using (var db = new RabbitDbEntities()) { rabbits = db.Rabbits.ToList(); } //=== MANUAL METHOD === //fancy 'lambda' - loops rabbits // - each loop(for each rabbit) adds an item(adds the rabbit) to the listbox //rabbits.ForEach(rabbit => ListBoxRabbits.Items.Add(rabbit)); //=== BINDING METHOD === //better way to bind the list and database ListBoxRabbits.ItemsSource = rabbits; TextBoxAge.IsReadOnly = true; TextBoxName.IsReadOnly = true; ButtonEdit.IsEnabled = false; ButtonDelete.IsEnabled = false; ButtonCancel.IsEnabled = false; ListBoxRabbits.Focus(); }
private void deleteConfirmation() { //=== 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(); //- Repopulate the listbox (re-read from the database) rabbits = db.Rabbits.ToList(); //gets rabbits ListBoxRabbits.ItemsSource = rabbits; //binds to listbox } } ButtonDelete.Content = "Delete"; ButtonDelete.IsEnabled = false; ButtonCancel.IsEnabled = false; ButtonAdd.IsEnabled = true; ListBoxRabbits.IsEnabled = true; TextBoxName.Text = ""; TextBoxAge.Text = ""; ListBoxRabbits.Focus(); }
private void KeyboardShortcuts(object sender, KeyEventArgs e) { if (Keyboard.IsKeyDown(Key.A) && Keyboard.IsKeyDown(Key.LeftShift)) { ButtonAdd_Click(sender, e); } if (Keyboard.IsKeyDown(Key.Delete) && ButtonDelete.IsEnabled) { ButtonDelete_Click(sender, e); } if (Keyboard.IsKeyDown(Key.Delete) && Keyboard.IsKeyDown(Key.LeftCtrl) && ButtonDeleteAll.IsEnabled) { ButtonDeleteAll_Click(sender, e); } //if (Keyboard.IsKeyDown(Key.LeftCtrl) && Keyboard.IsKeyDown(Key.E) && ButtonEdit.IsEnabled) { ButtonEdit_Click(sender, e); } //if (Keyboard.IsKeyDown(Key.LeftCtrl) && Keyboard.IsKeyDown(Key.Back)) //{ // ButtonCancel_Click(sender, e); //} if (Keyboard.IsKeyDown(Key.Enter)) { if (ButtonAdd.Content.Equals("Save")) { if (TextBoxName.Text.Length > 0 && TextBoxAge.Text.Length == 0) { TextBoxAge.Focus(); } else { saveAdditions(); ButtonEdit.IsEnabled = false; ButtonCancel.IsEnabled = false; ButtonDelete.IsEnabled = false; ListBoxRabbits.Focus(); } } if (ButtonDelete.IsEnabled && ButtonDelete.Content.Equals("Confirm Delete")) { deleteConfirmation(); ButtonAdd.IsEnabled = true; } if (ButtonDeleteAll.IsEnabled && ButtonDeleteAll.Content.Equals("Confirm Delete")) { DeleteAllConfirmation(); } } }