只添加一个图片框

时间:2017-12-05 10:09:51

标签: c# winforms

我需要添加一个图片框。但代码增加了无限的图片框。如果我的分数是10,我想只添加一个图片框。请帮我解决这个问题:

if (score == 10)
{
    PictureBox zombie = new PictureBox();
    {

       zombie.Tag = "zombie"; // add a tag to it called zombie
       zombie.Image = Properties.Resources.beer_PNG2331; 
       zombie.Left = rnd.Next(0, 900); 
       zombie.Top = rnd.Next(0, 800); 
       zombie.SizeMode = PictureBoxSizeMode.AutoSize; 
       this.Controls.Add(zombie); /
       player.BringToFront(); 
    }
}

1 个答案:

答案 0 :(得分:0)

编辑:改进回答者 删除大括号并确保软件中没有其他部分导致该部分代码以相同的分数值执行多次

if (score == 10)
{
    PictureBox zombie = new PictureBox();
    zombie.Tag = "zombie"; // add a tag to it called zombie
    zombie.Image = Properties.Resources.beer_PNG2331; 
    zombie.Left = rnd.Next(0, 900); 
    zombie.Top = rnd.Next(0, 800); 
    zombie.SizeMode = PictureBoxSizeMode.AutoSize; 

    this.Controls.Add(zombie); 

    player.BringToFront(); 
}