//3. Raise Event QuitBid private void OnEndBid(BidEventInfo e) { if (QuitBid != null) { QuitBid(this, e); } }
//3. Raise Event public void OnNewBid(BidEventInfo e) { if (NewBid != null) { NewBid(this, e); } }
//2. Determine when to raise the event. //Note: The arguments here, neither the sender, nor the e is of any interest for us, //as these two belong to the button-click event. //We create a bidInfo (correspondng to e) object and the sender is "this". private void btnSubmit_Click(object sender, RoutedEventArgs e) { decimal newBid = ToDecimal.GetDecimal(txtAmount.Text); BidEventInfo bidInfo = new BidEventInfo(this.Title, newBid); OnNewBid(bidInfo); //Rais event }
//2. When to raise EndBid event private void btnQuit_Click(object sender, RoutedEventArgs e) { if (MessageBox.Show("You'll not be able to place a bid through this session. Continue?", "Think twice", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { BidEventInfo bidInfo = new BidEventInfo(this.Title, 0.0m); OnEndBid(bidInfo); } }
/// <summary> /// Here is the power of delegates - who is the bidder - don't know, /// the sender tells us at run time /// Eventhandler for the sender's (bidder's) SendQuitBid event - what to do when the event is /// fired by the publisher object (a bidder class, see above) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> /// private void OnBidderDrawBack(object sender, BidEventInfo e) { lstBidHistory.Items.Add(string.Format("{0} draws back at {1}. Please come back!", e.Bidder, DateTime.Now.ToShortTimeString())); if (sender != null) { BidderIsPublisher frm = (BidderIsPublisher)sender; //Unubscribe to the publisher's new bid and quit bid events //Compare with above (subscription) frm.NewBid -= OnNewBidSent; frm.QuitBid -= OnBidderDrawBack; frm.Title = "You are out! " + DateTime.Now.ToShortTimeString(); } }
//############################################################################### /// <summary> /// From this line on no information available on the bidder that is just created. /// Here is the power of delegates - who is the bidder - don't know, /// the sender tells us at run time /// /// Eventhandler for the SendNewBid event - what to do when the event is /// fired by the publisher object (a bidder class, see above) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnNewBidSent(object sender, BidEventInfo e) { if (!AcceptBid()) { return; } string strError = "<= current amount - not valid! "; string strOut = string.Format("{0} places a new bid {1,0:f2}", e.Bidder, e.Amount); if (!CheckIfbidisValid(e.Amount)) { strOut += strError; } else { m_currentAmount = e.Amount; } lstBidHistory.Items.Add(strOut); }