Beispiel #1
0
        public void ButtonDelete_Click(object sender, EventArgs e)
        {
            if (ButtonDelete.Content.Equals("Delete"))
            {
                ButtonDelete.Content = "Confirm";
            }
            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); // creating new object and finding it's correlation in the database
                        db.Rabbits.Remove(rabbitToDelete);                     // remove the rabbit from the database
                        db.SaveChanges();                                      //  save changes to database

                        //ListBoxRabbit.ItemsSource = null;

                        // rabbit = null;
                        rabbits = db.Rabbits.ToList();
                        ListBoxRabbit.ItemsSource = rabbits;
                    }
                }
                TextBoxName.Text = "";
                TextBoxAge.Text  = "";

                ButtonDelete.IsEnabled = false;

                ButtonDelete.Content = "Delete";
            }
        }
Beispiel #2
0
        public void ButtonEdit_Click(object sender, EventArgs e)
        {
            if (ButtonEdit.Content.Equals("Edit")) // .Equals is required instead of ==
            {
                ButtonEdit.Background = brush2;
                ButtonEdit.Content    = "Save";
                // enable text boxes to be edited
                TextBoxName.IsReadOnly = false;
                TextBoxAge.IsReadOnly  = false;
                // change the colour of the textboxes while they are being edited
                TextBoxAge.Background  = (SolidColorBrush)Brushes.White;
                TextBoxName.Background = (SolidColorBrush)Brushes.White;
                // to keep the add button disabled
                ButtonAdd.IsEnabled = false;
                // to highlight the text of the rabbit name
                TextBoxName.Focus();
                TextBoxName.SelectAll();
            }
            else
            {
                ButtonEdit.Background = brush3;
                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);
                            // read 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 the binding rabbit
                            // ListBoxRabbit.ItemsSource = null; // remove binding
                            //ListBoxRabbit.Items.Clear();    // clear it out
                            // repopulate listbox // re-read from db
                            rabbits = db.Rabbits.ToList();       // get rabbits
                            ListBoxRabbit.ItemsSource = rabbits; // bind to listbox again
                        }
                    }
                    //TextBoxAge.Text = "";
                    //TextBoxName.Text = "";
                    TextBoxAge.IsReadOnly  = true;
                    TextBoxName.IsReadOnly = true;
                }
            }
        }
Beispiel #3
0
        private void ButtonAdd_Click(object sender, EventArgs e)
        {
            if (ButtonAdd.Content.Equals("Add"))
            {
                ButtonAdd.Content    = "Save";
                ButtonAdd.Background = brush2;
                // 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;
            }
            else
            {
                ButtonAdd.Content      = "Add";
                ButtonAdd.Background   = brush3;
                TextBoxAge.Background  = brush2;
                TextBoxName.Background = brush2;
                TextBoxName.IsReadOnly = true;
                TextBoxAge.IsReadOnly  = true;
                // commit changes
                if ((TextBoxAge.Text.Length > 0) && (TextBoxName.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 to the list
                        using (var db = new RabbitDbEntities())
                        {
                            db.Rabbits.Add(RabbitToAdd);
                            db.SaveChanges();
                            // to update the view

                            rabbit = null;

                            rabbits = db.Rabbits.ToList();
                            ListBoxRabbit.ItemsSource = null;
                            ListBoxRabbit.ItemsSource = rabbits;
                        }
                    }
                }
            }
        }