Jump to content

If/else-statement won't execute on PictureBox click event!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
Zolomon

Zolomon

    Newbie

  • Members
  • Pip
  • 6 posts
Hello!

I am creating a tic-tac-toe game as a windows form using PictureBoxes.
The problem currently have is that my if/else-statement won't execute unless
I have a MessageBox.Show(insertStringHere); before it. I have tried to use break points but I couldn't
make much out of it. It did work if I assigned a new image to the PictureBox as well, but I haven't found anything else.

I have tried to invalidate, update, refresh it, but nothing works. Sad

If anyone feels like taking a look at it, I would be forever grateful!
Source & project: http://www.zolomon.e...c/TicTacToe.rar -lines 56-84
[HIGHLIGHT="C#"]private void pbx1_Click(object sender, EventArgs e)
{
MessageBox.Show("");

// Will work only if I use above to assign a new image, bellow code

// won't work either.

/* Image myImage = ((PictureBox)sender).Image;
((PictureBox)sender).Image = myImage;
((PictureBox)sender).Refresh();*/

if (Check(((PictureBox)sender), imgBoard) == true)
{
if (CheckPlayerState(bPlayerState) == true)
{
((PictureBox)sender).Image = imgPlayer1;
tSSLStatus.Text = "Player 2's turn...";
bPlayerState = false;
}
else if (CheckPlayerState(bPlayerState) == false)
{
((PictureBox)sender).Image = imgPlayer2;
tSSLStatus.Text = "Player 1's turn...";
bPlayerState = true;
}
}
else if (Check(((PictureBox)sender), imgPlayer1))
{
MessageBox.Show("Tile already picked. Repick!");
}
else if (Check(((PictureBox)sender), imgPlayer2))
{
MessageBox.Show("Tile already picked. Repick!");
}

//((PictureBox)sender).Enabled = false;
}[/HIGHLIGHT]

Cheers,
Zolomon

#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
When you did the breaks did the if statements actually execute? I think this is just a problem with the images changing and not being updated. I can't open your program because you have done it in a new version of VS than I have.

Try this, click on the box (without the messagebox there). When it doesn't change move the window down all the way to your start bar (the title bar just above the start bar). Then pull it back up. This will force a redraw of the entire application. Does it change then?

#3
Zolomon

Zolomon

    Newbie

  • Members
  • Pip
  • 6 posts
I uploaded a version for VS2005 at TicTacToe_VS05.rar!

Thank you for looking into it for me but the problem isn't
what you pointed out since the if/else statement in the click event doesn't even execute, and I know this because I have tried to use
MessageBoxes to show if it's worked or not.

Apparently it might be because "==" will check equality for the reference of the images and not for the byte values, or something
similar (asked on other forums as well), but I couldn't make out
how to solve it anyhow...

#4
Guest_Jordan_*

Guest_Jordan_*
  • Guests
I should of seen the problem before. Remove

this.Invalidate();

Change:

if (Check(((PictureBox)sender), imgBoard) == true)

to

if (Check(((PictureBox)sender), imgBoard) == false)

And this works for me.

#5
Zolomon

Zolomon

    Newbie

  • Members
  • Pip
  • 6 posts
If I do that, change "if (Check(((PictureBox)sender), imgBoard) == true)" to false, then it destroys the whole purpose of the if/else-statement! :P

It's supposed to check if the current pictureBox has been used or not, if it hasn't, then it should assign a new image to it, otherwise it should say that it's already been used.

Thanks though!

#6
Guest_Jordan_*

Guest_Jordan_*
  • Guests
You have your logic wrong somewhere then. When I load your program and before I click on anything I set a break. I clicked on a square which had no image in it. The image changed. Did you try my suggestion or just repost?

If you look at your logic:

User Clicks
Check()
True -> Change Image
False -> Messagebox

Now, look at the Check() logic:
if (image = img)
true
else
false


So, you logic states: When check = true change the image. You are changing a changed image. Put those two logics together:

User Clicks:
Check()
if (pbx.Image == img)
True -> Change Image
if (image != img)
False -> Messagebox

So, if there is no image in the square you always receive a false. If there is an image you receive a true which messes up your whole logic.

#7
Zolomon

Zolomon

    Newbie

  • Members
  • Pip
  • 6 posts
I tried it and it works, yes, but not as I wish.
But then I become very flummoxed as it seems to work if I show a MessageBox first?

#8
Guest_Jordan_*

Guest_Jordan_*
  • Guests
IMO, you should rewrite this code. You are comparing against two images when you should create an array 3x3 and assign which block has been taken. If block [0][0] has been clicked then set the value to 1. If the value of the block is 1 when clicked then it is already taken.

#9
Zolomon

Zolomon

    Newbie

  • Members
  • Pip
  • 6 posts
Yeah, I guess you're right. :)
Thank you for your input!

#10
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Let me know if you need any help.