Windows Develop Bookmark and Share   
 index > Windows Forms General > Logical Problem in Picturebox
 

Logical Problem in Picturebox

I have following piece of code but some how something is wrong in logic in CheckAll() function.

The logical problem in the below code is that when user click on any picturebox it shows message "player 1 is winner".

I debugged the code what it does that when the user click any picturebox it check on CompareImages() function and if user clicked e.g. picturebox1 it will skip Ist comparison in CheckAll() function and when it reaches second comparison it will go in displaywinner() function.

Code Snippet

public bool CompareImages(Image image1,Image image2,Image image3)
{
if(!Object.Equals(image1,image2))
{
return false;
}
if(!Object.Equals(image2,image3))
{
return false;
}

return true;

}

public void checkAll()
{
if (CompareImages(box1.Image, box2.Image, box3.Image))
{
DisplayWinner();

}
else if (CompareImages(box4.Image, box5.Image, box6.Image))
{
DisplayWinner();

}
else if (CompareImages(box7.Image, box8.Image, box9.Image))
{
DisplayWinner();

}
else if (CompareImages(box1.Image, box5.Image, box9.Image))
{
DisplayWinner();

}
else if (CompareImages(box3.Image, box5.Image, box7.Image))
{
DisplayWinner();

}
else if (CompareImages(box1.Image, box4.Image, box7.Image))
{
DisplayWinner();

}
else if (CompareImages(box2.Image, box5.Image, box8.Image))
{
DisplayWinner();

}
else if (CompareImages(box3.Image, box6.Image, box9.Image))
{
DisplayWinner();

}

}

public void DisplayWinner()
{
if (player == 2) player = 1;
else player = 2;
MessageBox.Show("Player" + player + "wins" );
reset();
displayAgain();
}
}
private void switchPlayers()
{
if(player == 1 )
player=2;
else if(player==2)
player=1;

checkAll();
}

shah123  Thursday, May 31, 2007 3:34 PM
At first, all the images are going to be either a default value that you set or null, meaning that at the first click, there will be a "winning" condition as three images are the same.

Just put in your CompareImages something like

if((image1== null) || (image2==null) || (image3==null))
{
return false;
}


Another interesting fact is your use of Object.Equals which will only be true if the two Images reference the same instance. That is, they not only have the same value, but are the same object. If you haven't kept a reference to each players Image and are assigning the picture box images with that, this will always be false (except when the images are null, which is what you get now)

A little rambling I'll admit, but hopefully that will help.

Good luck,

-mwalts
mwalts  Thursday, May 31, 2007 4:25 PM
At first, all the images are going to be either a default value that you set or null, meaning that at the first click, there will be a "winning" condition as three images are the same.

Just put in your CompareImages something like

if((image1== null) || (image2==null) || (image3==null))
{
return false;
}


Another interesting fact is your use of Object.Equals which will only be true if the two Images reference the same instance. That is, they not only have the same value, but are the same object. If you haven't kept a reference to each players Image and are assigning the picture box images with that, this will always be false (except when the images are null, which is what you get now)

A little rambling I'll admit, but hopefully that will help.

Good luck,

-mwalts
mwalts  Thursday, May 31, 2007 4:25 PM
EXCELLENT EXCELLENT..IT WORKED
shah123  Thursday, May 31, 2007 4:33 PM

You can use google to search for other answers

Custom Search

More Threads

• Emergent!!!! List<Type> variable addition problem
• Changing Tooltip text
• how to change the shorcuts's icon
• Displaying MessageBox when disabled menu item is clicked
• How can I bind a Dataset to the controls on a form to save data a user enters in the form?
• Dataset not accepted into FilePutObject
• localizable UserControl best way to do it.
• binding the textbox to grid
• Flash Player
• Windows properties??