0

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;
        }

       .............................
Bill789
  • 27
  • 4
  • 1
    How do you click on `PictureBox` if it's not visible? Most likely `sender` is not `PictureBox`. Print `sender.GetType()`. – Reputation Farmer Feb 23 '19 at 21:50
  • That makes sense, when I print it shows that it is indeed the TableLayoutPanel, is there another way to accomplish what I need? ie have the pictures hidden at the start of the game then have them show up when clicked? – Bill789 Feb 23 '19 at 22:11
  • Thanks, I overlayed another picturebox over the first one and that worked, – Bill789 Feb 23 '19 at 23:33

0 Answers0