private void ChallengersListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { //Remember SelectedIndex, as SelectionChanged() runs every Timer_Elapsed() run if (ChallengersListBox.SelectedIndex == -1 || ChallengersListBox.SelectedIndex == SelectedIndex) { ChallengersListBox.SelectedIndex = SelectedIndex; return; } //User selected a new index SelectedIndex = ChallengersListBox.SelectedIndex; //Try finding a challenger corresponding to user index, and update UI to challenger data try { SelectedChallenger = Challengers[SelectedIndex]; ChallengerTextBox.Text = SelectedChallenger.Name; GymType_Clicked(new Button() { Content = SelectedChallenger.GymType }, null); Gym_Clicked(new List <Button>(RegularGyms) .AddInto(AdvancedGyms) .Find(btn => btn.Content.ToString() == SelectedChallenger.Gym), null); } //Couldn't find Challenger in this.Challengers - SelectedChallenger gets reset with UI catch (ArgumentOutOfRangeException) { SetUI(true); } catch (Exception ex) { throw ex; } }
/// <summary> /// Sets Gymlines & Minute inputs, adds another line to <see cref="ChallengersListBox"/> if needed, resets <see cref="ChallengerTextBox"/> /// </summary> /// <param name="Autocalled">Wether method was autocalled via <see cref="ChallengersListBox_SelectionChanged(object, SelectionChangedEventArgs)"/></param> private void SetUI(bool Autocalled) { SetRegularGyms("Electric", "Bug", "Fairy", "Steel", "Water", "Dark", "Flying", "Dragon"); SetAdvancedGyms("Dark/Poison", "Fire/Dragon", "Electric/Ice", "Psychic/Dragon", "Water/Steel", "Electric/Flying", "Psychic/Fairy", "Steel/Fairy"); SetMinuteListBoxItems(); ChallengerTextBox.Text = "Challenger IGN"; //If SetUI wasn't autocalled, add a new line if (!Autocalled) { ChallengersListBox.Items.Add(""); } //else Selection_Changed() ran ArugmentOutOfRange exception, being unable to find a challenger in empty row else { SelectedChallenger = new Challenger(); } ChallengersListBox.SelectedIndex = ChallengersListBox.Items.Count - 1; }
private void StartButton_Click(object sender, RoutedEventArgs e) { //Any of these return true, user didn't provide enough input if (SelectedChallenger.Gym is null || ChallengerTextBox.Text == "Challenger IGN" || ChallengerTextBox.Text == string.Empty) { MessageBox.Show("Not all requirements were met"); return; } //Set SelectedChallenger SelectedChallenger.Name = ChallengerTextBox.Text; SelectedChallenger.EndTime = DateTime.Now.AddMinutes(int.Parse(MinuteListBox.Items[MinuteListBox.SelectedIndex].ToString())); //If start was used to update a previous Challenger, replace previous value try { ChallengersListBox.Items[ChallengersListBox.Items.Count - 1] = SelectedChallenger.ToString(); } //Challenger is a new challenger catch (ArgumentOutOfRangeException) { //If challenger is not a duplicate, add challenger to ListBox if (!ChallengersListBox.Items.Contains(SelectedChallenger.GetToString())) { ChallengersListBox.Items.Add(SelectedChallenger.ToString()); } } finally { //If challenger is not a duplicate, add chalenger to this.Challengers if (!Challengers.Contains(SelectedChallenger)) { Challengers.Add(SelectedChallenger); } } //Reset SelectedChallenger SelectedChallenger = new Challenger(); //Reset UI SetUI(false); }