我是C#的新手,正在尝试编写小型存储卡游戏。我有一个内部有16个图片框的TableLayouPanel,每当我使图片框不可见时(设置游戏图像时),我的事件方法都显示单击图片框时我的图片框为空。
如果我在设置游戏时保持图像可见,则事件方法将图片框视为非空。
在下面查看我的代码段:
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;
}
.............................