/// <summary> /// updates radio buttons content /// Checks which radio button content to update by checking bettors name /// or viewing which radio button has been clicked. /// </summary> /// <param name="bettor"></param> /// <returns></returns> private RadioButton DetermineBettorUi(Bettor bettor) { if ((bool)_rbtnBettor1.IsChecked || bettor.Name == "Joe") { _rbtnBettor1.Content = $"{bettor.Name} has {bettor.Cash} bucks"; _uiBettor = _rbtnBettor1; return(_uiBettor); } else if ((bool)_rbtnBettor2.IsChecked || bettor.Name == "Bob") { _rbtnBettor2.Content = $"{bettor.Name} has {bettor.Cash} bucks"; _uiBettor = _rbtnBettor2; return(_uiBettor); } else if ((bool)_rbtnBettor3.IsChecked || bettor.Name == "Anna") { _rbtnBettor3.Content = $"{bettor.Name} has {bettor.Cash} bucks"; return(_uiBettor = _rbtnBettor3); } else { Debug.Assert(false, "Unexpected bettor selector control. Cannot select the current bettor"); return(null); } }
/// <summary> /// Event handler when the user selects the current bettor using one of the /// three radio buttons. The same event handler is shared by all three radio /// buttons to avoid code duplication /// </summary> /// <param name="sender">one of the three radio buttons that represent the bettors</param> /// <param name="e">not used</param> private void OnBettorSelectorChecked(object sender, RoutedEventArgs e) { //check the sender of the event and select the correct bettor in the list //alternatively you can use the Tag property of the radio buttons by setting //it to the appropriate bettor object when the objects are created and here use //only extract the correct bettor from the Tag property value. See the final branch //for this solution if (sender == _rbtnBettor1) { _crtSelBettor = _bettorList[0]; } else if (sender == _rbtnBettor2) { _crtSelBettor = _bettorList[1]; } else if (sender == _rbtnBettor3) { _crtSelBettor = _bettorList[2]; } else { Debug.Assert(false, "Unexpected bettor selector control. Cannot select the current bettor"); return; } //use the current bettor information to display the bet information _txtCrtBettorName.Text = $"{_crtSelBettor.Name} bets"; }
/// <summary> /// Checks Bet description and sets it to initial value when program runs /// and then updates after a bettor has placed a bet /// </summary> /// <param name="bettor"></param> /// <returns></returns> private TextBlock DetermineBetDescUi(Bettor bettor) { _uiBetDesc = new TextBlock(); //checks if a bettor has placed a bet and then updates bettors label if (bettor.HasPlacedBet == true) { _uiBetDesc.Text = $"{bettor.Name} bets {_txtBetAmount.Text}$ on dog #{_cmbRaceHoundNo.SelectedIndex+1}"; return(_uiBetDesc); } { _uiBetDesc.Text = $"{bettor.Name} hasn't placed a bet"; return(_uiBetDesc); } }
/// <summary> /// the follwoing calls DetermineBettorUi and checks which radiobutton has been selected to update /// it equivalent _txtBet label /// </summary> private void UpdateBettorLabels(Bettor bettor) { RadioButton radioBettor = DetermineBettorUi(bettor); if (radioBettor == _rbtnBettor1) { _txtBet1.Text = DetermineBetDescUi(bettor).Text; } else if (radioBettor == _rbtnBettor2) { _txtBet2.Text = DetermineBetDescUi(bettor).Text; } else if (radioBettor == _rbtnBettor3) { _txtBet3.Text = DetermineBetDescUi(bettor).Text; } }
public MainPage() { //The current bettor is set by the event handler that is triggered when the //radio button is checked, which is done in the following call to //InitializeComponent() _crtSelBettor = null; this.InitializeComponent(); //initialize the timer field variable and setup a timer event handler that is called //at a given time interval _tmRaceTimer = new DispatcherTimer(); _tmRaceTimer.Tick += OnRaceTimerTick; _tmRaceTimer.Interval = TimeSpan.FromMilliseconds(25); //TODO: define a constant field variable for 100 and use it here //create the randomizer for all objects of the form _formRandomizer = new Random(); //NOTE it is important for the bettors and race hounds to be created AFTER //the controls have been created by the InitializeComponent() method CreateBettors(); CreateRaceHounds(); }