O'Reilly Forums: A Day At The Races - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

A Day At The Races MessageBox

#1 User is offline   Pierre_1 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 04-September 14

Posted 04 September 2014 - 02:31 PM

I am encountering a problem with the message box displaying the dog winner. My dogs run well in a randomly fashion, and when the race is over, the message box shows the correct winner. However when I press OK, another message box appears saying "Dog # x won the race!". I have to do that two more times (each time with a different dog) before being able to start a new race. It seems silly enough, but I don't understand why it does that. Any help will be much appreciated.


private void startRaceButton_Click(object sender, EventArgs e)
{
foreach (Greyhound dog in greyhoundArray)
{
dog.takestartingPosition();
}

timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
int winDog = 0;

for (int i = 0; i < greyhoundArray.Length; i++)
{
greyhoundArray[i].Run();

if (dog1PictureBox.Left >= raceTrackPictureBox.Width - dog1PictureBox.Width ||
dog2PictureBox.Left >= raceTrackPictureBox.Width - dog2PictureBox.Width ||
dog3PictureBox.Left >= raceTrackPictureBox.Width - dog3PictureBox.Width ||
dog4PictureBox.Left >= raceTrackPictureBox.Width - dog4PictureBox.Width)
{
timer1.Stop();
winDog = i + 1;
MessageBox.Show("Dog # " + winDog + " won the race!", "Race Over");

}
}
}
}
}
0

#2 User is offline   AndrewStellman 

  • Andrew Stellman
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 804
  • Joined: 08-October 08
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

Posted 06 September 2014 - 08:16 AM

You're calling MessageBox.Show() inside your for loop, so it will be called every time the loop executes.. Move it outside the loop.
Andrew Stellman
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
0

#3 User is offline   Pierre_1 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 04-September 14

Posted 06 September 2014 - 11:04 AM

Hello Andrew:

Thank you for your answer. It is also what I thought at first, but by moving the message box out of the for loop, I add even more message boxes. After erring quite a bit, I eventually found the solution (at least I believe so!). My mistake was to run a check on each individual dog picture box in the for loop. So as soon as I had a winner, and clicked OK on the message, the loop was checking the next dog picture box, and so on. I modified my code as follows, and everything seems working well :) :

private void timer1_Tick(object sender, EventArgs e)
{
startRaceButton.Enabled = false;
bettorNameLabel.Enabled = false;
int winDog = 0;

for (int i = 0; i < greyhoundArray.Length; i++)
{
greyhoundArray[i].Run();

if (greyhoundArray[i].myPictureBox.Right >= raceTrackPictureBox.Width)
{
timer1.Stop();
winDog = i + 1;
MessageBox.Show("Dog # " + winDog + " won the race!", "Race Over");

startRaceButton.Enabled = true;
bettorNameLabel.Enabled = true;

for (int index = 0; index < guyArray.Length; index++)
{
guyArray[index].collect(winDog);
guyArray[index].clearBet();
guyArray[index].updateLabels();
}
}
}
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users