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
If/else-statement won't execute on PictureBox click event!
Started by Zolomon, Jan 23 2008 07:51 AM
9 replies to this topic
#1
Posted 23 January 2008 - 07:51 AM
|
|
|
#2
Guest_Jordan_*
Posted 24 January 2008 - 06:24 AM
Guest_Jordan_*
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?
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
Posted 24 January 2008 - 06:59 AM
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...
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_*
Posted 24 January 2008 - 07:10 AM
Guest_Jordan_*
I should of seen the problem before. Remove
Change:
to
And this works for me.
this.Invalidate();
Change:
if (Check(((PictureBox)sender), imgBoard) == true)
to
if (Check(((PictureBox)sender), imgBoard) == false)
And this works for me.
#5
Posted 24 January 2008 - 07:14 AM
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!
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_*
Posted 24 January 2008 - 07:22 AM
Guest_Jordan_*
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.
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
Posted 24 January 2008 - 07:41 AM
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?
But then I become very flummoxed as it seems to work if I show a MessageBox first?
#8
Guest_Jordan_*
Posted 24 January 2008 - 07:47 AM
Guest_Jordan_*
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
Posted 24 January 2008 - 07:59 AM
Yeah, I guess you're right. :)
Thank you for your input!
Thank you for your input!
#10
Guest_Jordan_*
Posted 24 January 2008 - 08:21 AM
Guest_Jordan_*
Let me know if you need any help.


Sign In
Create Account

Back to top









