Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / July 2007

Tip: Looking for answers? Try searching our database.

ClickHandler bug

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Joseph - 19 Jul 2007 23:18 GMT
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;
               }      
           }
       }
Peter Duniho - 20 Jul 2007 01:55 GMT
> [...] 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
Joseph - 20 Jul 2007 04:04 GMT
Therein lies the problem. The Clickhandler method executes a second time on
its own instead of returning control to the form.

> > [...] What is happening when I step through the code is that the
> > ClickHandler event handler fires off its code as usual and loops back  
[quoted text clipped - 6 lines]
>
> Pete
Peter Duniho - 20 Jul 2007 05:36 GMT
> 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
Joseph - 20 Jul 2007 07:12 GMT
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.

> > Therein lies the problem. The Clickhandler method executes a second time  
> > on
[quoted text clipped - 20 lines]
>
> Pete
Chris Dunaway - 20 Jul 2007 14:22 GMT
> 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
Joseph - 20 Jul 2007 17:18 GMT
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();  
       }

> > 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
[quoted text clipped - 4 lines]
>
> Chris
Peter Duniho - 20 Jul 2007 17:31 GMT
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.