I've written a memory game Windows application that consists of 16 pictures
and the object of the game is to find all the matching pairs of cards. When
you first start the game everything works fine. If a card match is found both
cards are removed. If the two cards selected are not matches then the cards
turn back over. However, I have a problem when I click on a button to start a
second game. And the problem is this: The first card selection turns over
before the second card selection can be made. I've looked through my code and
I cannot figure out why this is happening. I've reset all variables and
boolean values back to the same values as when the program first loaded. I
think I've narrowed the source of the problem down to the ClickHandler event
handler. What is happening when I step through the code is that the
ClickHandler event handler fires off its code as usual and loops back through
the event again instead of returning to the app. Below is the ClickHandler
code. Any suggestions or ideas are definitely welcomed. Thanks
private void ClickHandler(Object sender, System.EventArgs e)
{
//when a pic box is clicked on, we do something
//we use the .Tag property to get to the Image property.
int Index =
Convert.ToInt32(((System.Windows.Forms.PictureBox) sender).Tag);
if (intNumMatches < conMaxMatches)
{
if (blnfirstTurn)
{
// this is the first pick
PicBoxes[Index].Image =
Image.FromFile(Directory.GetCurrentDirectory() + @"\pictures\" +
PicLabels[Index].Text);
//PicBoxes[Index].Image =
System.Drawing.Image.FromFile(PicBoxes[Index].Text);
PicBoxes[Index].Enabled = false; //turn off response to
click
blnfirstTurn = false;
intFirstPick = Index;
lblPick1.Text = get_flag_name(Index);
return;
}
else
{
//This is the second pick - test for match
PicBoxes[Index].Image =
Image.FromFile(Directory.GetCurrentDirectory() + @"\pictures\" +
PicLabels[Index].Text);
PicBoxes[Index].Enabled = false; //turn off response to
click
lblPick2.Text = get_flag_name(Index);
test_for_match(intFirstPick, Index);
blnfirstTurn = true;
++intNumTries;
lblNumTries.Text = Convert.ToString(intNumTries);
return;
}
}
}