1

我需要用 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();
    }
}
4

3 回答 3

-1

尝试这个

 List<PictureBox> pictureBoxList = new List<PictureBox>(10);
    int ID = 0;
    int position = 0;
    private void metroButton1_Click(object sender, EventArgs e)
    {
        ID++;
        pictureBoxList.Add((PictureBox)Controls.Find("pictureBox" + ID, true)[0]);
        pictureBoxList[position].Location = new System.Drawing.Point(1, 90);
        pictureBoxList[position].Name = "pictureBoxEnemy";
        pictureBoxList[position].Size = new System.Drawing.Size(25, 25);
        pictureBoxList[position].BackgroundImage = Properties.Resources.Enemy;
        pictureBoxList[position].BackgroundImageLayout = ImageLayout.Zoom;
        pictureBoxList[position].BringToFront();
        position++;



    }
于 2019-11-16T10:36:38.107 回答
-1

首先List<PictureBox> pictureBoxList = new List<PictureBox>(10);,10 是列表的最大长度,它没有填充图片框。因此,当您第一次单击按钮时,当您只有一个元素时,您的 id 为 1,因此它显然会给出和异常。它应该是

pictureBoxList.Add((PictureBox)Controls.Find("pictureBox" + ID+1/* If your picture box is called pictureBOx1 and not pictureBox0 */, 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();
ID++;

当然,这是假设 Control.Find 返回一个有效值而不是 null,但是您将遇到对象引用异常。

于 2019-11-16T10:41:37.447 回答
-2

向 Controls 集合添加一个守卫,这样您就不会访问不存在的数组元素。这意味着在使用“[0]”访问之前检查 Controls.Find 是否实际包含项目。我在下面举一个例子。希望它可以帮助你。

        private const int MAX_ENEMY = 9;
        private void buttonAddEnemy_Click(object sender, EventArgs e)
        {
            if (ID < MAX_ENEMY)
            {
                Control picBoxControl = Controls.Find("pictureBox" + ID, true)[0];
                if (picBoxControl is PictureBox)
                {
                    pictureBoxList.Add(picBoxControl as PictureBox);
                    //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();
                    ID++;
                }
            }
        }    
于 2019-11-16T11:24:53.280 回答