我需要用 C# 中的动画制作一些“游戏”。所以我需要按一个按钮来创建新的图片框,我试图让它与一个列表一起工作,但我想念一些东西。数组边界之外的索引,我需要设置错误告诉我的图片框列表的长度,但它不起作用。
public partial class Form1 : Form
{
List<PictureBox> pictureBoxList = new List<PictureBox>(10);
int ID = 0;
private void buttonAddEnemy_Click(object sender, EventArgs e)
{
ID++;
pictureBoxList.Add((PictureBox)Controls.Find("pictureBox" + ID, true)[0]);
pictureBoxList[ID].Location = new System.Drawing.Point(1, 90);
pictureBoxList[ID].Name = "pictureBoxEnemy";
pictureBoxList[ID].Size = new System.Drawing.Size(25, 25);
pictureBoxList[ID].BackgroundImage = Properties.Resources.Enemy;
pictureBoxList[ID].BackgroundImageLayout = ImageLayout.Zoom;
pictureBoxList[ID].BringToFront();
}
}