C# Picturebox 未加载代码中添加的

C# Picturebox not loading that is added in code

我有一个在播放器图片框内绘制的程序,如下:

public static PictureBox drawPlayer(Engine.Tile location, Image looks)
    {
        PictureBox player = new PictureBox();
        player.Location = location.img.Location;
        player.BackColor = Color.Yellow;
        player.Size = new Size(50, 50);
        player.Name = "Player";
        player.Visible = true;
        player.BringToFront();

        return player;
    }

但是,它不起作用,它是这样调用的:

t.Controls.Add(drawPlayer(location, image));

如果能对此提供任何帮助或回答为什么这不起作用,我暂时将框的颜色设置为黄色只是为了让它在它起作用时真正脱颖而出最后决定加载

谢谢,
劳伦斯

您需要在将控件添加到表单后使用player.BringToFront();,因为当您在制作图片框的方法中调用它时,它不存在于表单上。