ClickHandler bug

G

Guest

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;
}
}
}
 
P

Peter Duniho

[...] 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.

What do you mean "loops back through the event again"? There's no loop in
your ClickHandler() method. How could it loop as you step through it in
the debugger?

Pete
 
G

Guest

Therein lies the problem. The Clickhandler method executes a second time on
its own instead of returning control to the form.

Peter Duniho said:
[...] 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.

What do you mean "loops back through the event again"? There's no loop in
your ClickHandler() method. How could it loop as you step through it in
the debugger?

Pete
 
P

Peter Duniho

Therein lies the problem. The Clickhandler method executes a second time
on
its own instead of returning control to the form.

So it's _not_ looping, in spite of what you wrote?

Is your question asking why the ClickHandler() method is being called for
the same control twice?

Unless you think that the ClickHandler() itself is causing that to happen,
you haven't posted enough code to answer the problem.

At the same time, posting your entire program is unlikely to solicit a lot
of responses. It will almost certainly be more than anyone wants to look
at at once. So you need to reduce the problem to the bare minimum, yet
complete sample of code that will reproduce the problem.

In doing so, you'll probably find the problem. But if not, you can then
post that smaller subset of code and maybe someone can find your bug.

Personally, I'd look at the call stack for both Click events and try to
figure out what prompted the extra one.

Pete
 
G

Guest

Yes, basically in a nutshell that is my question: Why the ClickHandler()
method is being called for the same control twice? I like your idea of
looking at the call stack, I'll try that. Thanks for replying.
 
C

Chris Dunaway

Yes, basically in a nutshell that is my question: Why the ClickHandler()
method is being called for the same control twice? I like your idea of
looking at the call stack, I'll try that. Thanks for replying.
Sounds like you wired up the handler twice. When you reset the game
for a new game, is it possible that you called AddHandler again?

Chris
 
G

Guest

Chris,

I am pretty sure that is not the case, here is the code that is used when
the form first loads (and works ok) and the method for a new game:

private void Form1_Load(object sender, EventArgs e)
{

/////////////////////////////////////////////////
blnfirstTurn = true; //Sets the value to true for the first pick
intNumMatches = 0; //increments each time a pair is found
intNumTries = 0;

lblMsg.Text = "Click a box";
lblPick1.Text = "";
lblPick2.Text = "";
set_up_blanks();
load_names();
swap_names(); //Shuffles the names
/////////////////////////////////////////////////

}

private void cmdStart_Click(object sender, EventArgs e)
{
//Here we are resetting for a new game
blnfirstTurn = true;
intNumMatches = 0;
intNumTries = 0;

lblMsg.Text = "Click a box";
lblPick1.Text = "";
lblPick2.Text = "";
set_up_blanks();
load_names();
swap_names();
}
 
P

Peter Duniho

Chris,

I am pretty sure that is not the case, here is the code that is used when
the form first loads (and works ok) and the method for a new game:

For what it's worth, while I don't see anything obvious in the code you
posted, it's really not a good idea to duplicate code like that.

Put all of your shared initialization code into a single function, and
then call that function from both places.

This is better for a variety of reasons, but IMHO the biggest reason is
that if you have a bug in the shared code, you only have to fix it once.
:)

This has nothing to do with your actual question, but should help you in
the long run.

As far as the code you posted goes, it's impossible to know for sure that
nothing odd is going on, because you have three different functions called
during initialization that you didn't post. So something could be
happening in those functions that is causing a problem. Maybe not, but we
don't know.

I still think the debugger is your best bet. It's possible that someone
might be able to identify your problem by code inspection, but you've got
the whole program there and running, and watching what it does as it runs
is likely to be the best way to solve an issue like this.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top