ClickHandler bug

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?Sm9zZXBo?=

    #1

    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(Ob ject sender, System.EventArg s 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.Windo ws.Forms.Pictur eBox)sender).Ta g);

    if (intNumMatches < conMaxMatches)
    {

    if (blnfirstTurn)
    {
    // this is the first pick
    PicBoxes[Index].Image =
    Image.FromFile( Directory.GetCu rrentDirectory( ) + @"\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(I ndex);
    return;
    }
    else
    {
    //This is the second pick - test for match
    PicBoxes[Index].Image =
    Image.FromFile( Directory.GetCu rrentDirectory( ) + @"\pictures\ " +
    PicLabels[Index].Text);
    PicBoxes[Index].Enabled = false; //turn off response to
    click
    lblPick2.Text = get_flag_name(I ndex);
    test_for_match( intFirstPick, Index);
    blnfirstTurn = true;
    ++intNumTries;
    lblNumTries.Tex t = Convert.ToStrin g(intNumTries);
    return;
    }
    }
    }





  • Peter Duniho

    #2
    Re: ClickHandler bug

    On Thu, 19 Jul 2007 15:18:03 -0700, Joseph
    <Joseph@discuss ions.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

    Comment

    • =?Utf-8?B?Sm9zZXBo?=

      #3
      Re: ClickHandler bug

      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
      <Joseph@discuss ions.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
      >

      Comment

      • Peter Duniho

        #4
        Re: ClickHandler bug

        On Thu, 19 Jul 2007 20:04:13 -0700, Joseph
        <Joseph@discuss ions.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

        Comment

        • =?Utf-8?B?Sm9zZXBo?=

          #5
          Re: ClickHandler bug

          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
          <Joseph@discuss ions.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
          >

          Comment

          • Chris Dunaway

            #6
            Re: ClickHandler bug

            On Jul 20, 1:12 am, Joseph <Jos...@discuss ions.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

            Comment

            • =?Utf-8?B?Sm9zZXBo?=

              #7
              Re: ClickHandler bug

              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(obje ct 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...@discuss ions.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
              >
              >

              Comment

              • Peter Duniho

                #8
                Re: ClickHandler bug

                On Fri, 20 Jul 2007 09:18:07 -0700, Joseph
                <Joseph@discuss ions.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

                Comment

                Working...