473,486 Members | 2,424 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

ClickHandler bug

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

Jul 19 '07 #1
7 1909
On Thu, 19 Jul 2007 15:18:03 -0700, Joseph
<Jo****@discussions.microsoft.comwrote:
[...] 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
Jul 20 '07 #2
Therein lies the problem. The Clickhandler method executes a second time on
its own instead of returning control to the form.

"Peter Duniho" wrote:
On Thu, 19 Jul 2007 15:18:03 -0700, Joseph
<Jo****@discussions.microsoft.comwrote:
[...] 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
Jul 20 '07 #3
On Thu, 19 Jul 2007 20:04:13 -0700, Joseph
<Jo****@discussions.microsoft.comwrote:
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
Jul 20 '07 #4
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.

"Peter Duniho" wrote:
On Thu, 19 Jul 2007 20:04:13 -0700, Joseph
<Jo****@discussions.microsoft.comwrote:
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
Jul 20 '07 #5
On Jul 20, 1:12 am, Joseph <Jos...@discussions.microsoft.comwrote:
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

Jul 20 '07 #6
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();
}

"Chris Dunaway" wrote:
On Jul 20, 1:12 am, Joseph <Jos...@discussions.microsoft.comwrote:
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

Jul 20 '07 #7
On Fri, 20 Jul 2007 09:18:07 -0700, Joseph
<Jo****@discussions.microsoft.comwrote:
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
Jul 20 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
39442
by: Dipin | last post by:
Hi All; I have this javascript which is adding a new button to the column in the row which is created dynamically, the innerhtml shows that the onclick event is correctly added but it never gets...
1
2336
by: Manco | last post by:
I've read that events are a C# implementation of the Observer design pattern which states that: There is a source object providing an event, subscriber objects can subscribe to the event and the...
10
2485
by: Sean Dockery | last post by:
I have the following HTML file that I've been using for testing... <html> <head> <script type="text/javascript"> <!-- function handleWindowLoad() { var items = ; for (var i = 0; i < 11; i++)...
6
6845
by: Jeremy | last post by:
I want each instance of an object to be able to listen for input events. When the event occurs, a method of the object should be called, such that "this" is in scope and refers to the object...
0
1254
by: JohnGoogle | last post by:
I've downloaded the source code for RSS Bandit from SourceForge.net (version 1.3.0.38) which was written in C#. I just want to analyse the code to pick up coding tricks etc. I've tried to build...
2
1415
by: Thorsten Adams | last post by:
Hello newsgrouplers! In C# there is a code EControl window = ControlDeclarationManager.Instance.CreateControl( "Gui\\MessageBoxWindow.gui" ); Controls.Add( window ); window.Controls.Text =...
2
1308
by: EagerToKnow | last post by:
I am trying to have it cleaner. I have two files : 1) linkHandler.html 2)linkHandler.js I am trying to call a function that is there in js file from within html file for an 'onClick' event. ...
2
1656
by: alhalayqa | last post by:
hi, in MSIE, you can attach onclick event to both anchor and document objects. here is some code : document.onclick = function() {alert('document clicked ...'); } <a href= " " ...
13
1370
by: =?Utf-8?B?Smlt?= | last post by:
In a C# project I have an array of PictureBoxes. I form an event handler for each picturebox as it is created using i as an index. PictureBox1.Click += new System.EventHandler(ClickHandler); In...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7330
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5434
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4865
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3070
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.