I am new to C# and was trying to write a small memory card game. I have a TableLayouPanel with 16 picture boxes inside, whenever I make the picture boxes invisible (when I setup the game images) my event method shows that my picture boxes are null when picture boxes are clicked.
If I leave the images visible when I setup the game then the event method sees the picture boxes as not null.
See my snippet of my code below:
private void AssignIconsToSquare()
{
foreach (Control control in gameIconTable.Controls)
{
PictureBox picture = control as PictureBox;
if (picture != null)
{
int randomNumber = random.Next(symbols.Count);
picture.Image = symbols[randomNumber];
symbols.RemoveAt(randomNumber);
picture.Visible = false;
Console.WriteLine(picture == null); // this is false here
}
}
}
private void pictureClick(object sender, EventArgs e)
{
PictureBox clickedImage = sender as PictureBox;
Console.WriteLine(clickedImage == null); // this is true here
if (clickedImage != null) // does not get past this line
{
if (clickedImage.Visible == true)
{
return;
}
if (firstSymbolClicked.Visible == false)
{
firstSymbolClicked.Visible = true;
return;
}
.............................