/// <summary> /// Changes the move behavior of the specified animal. /// </summary> /// <param name="sender">The object that initiated the event.</param> /// <param name="e">The arguments of the event.</param> private void changeMoveBehaviorButton_Click(object sender, RoutedEventArgs e) { Animal animal = this.animalListBox.SelectedItem as Animal; object behaviorType = this.moveBehaviorTypeComboBox.SelectedItem; if (animal != null && behaviorType != null) { animal.MoveBehavior = MoveBehaviorFactory.CreateMoveBehavior((MoveBehaviorType)behaviorType); } else { MessageBox.Show("Select an animal and a move behavior type to change its move behavior."); } }
/// <summary> /// Changes move behavior of the animal. /// </summary> /// <param name="sender">The object that initiated the event.</param> /// <param name="e">The event arguments of the event.</param> private void changeMoveBehaviorButton_Click(object sender, RoutedEventArgs e) { try { object behaviorType = this.changeMoveBehaviorComboBox.SelectedItem; Animal animal = this.animalListBox.SelectedItem as Animal; if (animal != null && behaviorType != null) { IMoveBehavior newmovebehavior = MoveBehaviorFactory.CreateMoveBehavior((MoveBehaviorType)behaviorType); animal.MoveBehavior = newmovebehavior; } } catch (NullReferenceException) { MessageBox.Show("Please select a behavior type and an animal to change its move behavior."); } }
/// <summary> /// Changes the behavior of an animal. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void changeBehaviorButton_Click(object sender, RoutedEventArgs e) { // Get the animal selected from the list box. Animal animal = (Animal)this.animalListBox.SelectedItem; // Gets the behavior selected from the combo box. MoveBehaviorType behavior = (MoveBehaviorType)this.changeBehaviorComboBox.SelectedItem; if (animal != null && behavior != null) { // Create an IMoveBehavior using the MoveBehaviorFactory. IMoveBehavior newBehavior = MoveBehaviorFactory.CreateMoveBehavior(behavior); // Set the aniaml's behavior to the new behavior. animal.MoveBehavior = newBehavior; } }
/// <summary> /// Changes the move behavior via the button click. /// </summary> /// <param name="sender">System data.</param> /// <param name="e">Associated event data.</param> private void changeMoveBehaviorButton_Click(object sender, RoutedEventArgs e) { try { // Get the selected animal in the list box. Animal animal = this.animalListBox.SelectedItem as Animal; MoveBehaviorType moveBehaviorType = (MoveBehaviorType)this.changeMoveBehaviorComboBox.SelectedItem; if (animal != null) { IMoveBehavior animalMove = MoveBehaviorFactory.CreateMoveBehavior(moveBehaviorType); animal.MoveBehavior = animalMove; } } catch (NullReferenceException) { MessageBox.Show("An animal and a behavior both need to be selected."); } }