C# 益智游戏索引超出范围异常

C# 益智游戏索引超出范围异常,c#,windows,C#,Windows,只有将游戏设置从4个边长更改为9/16/25时,才会发生此异常。提前谢谢 int LengthSides = Properties.Settings.Default.NumberOfSquares; Image image; PictureBox picBoxWhole = null; PictureBox[] picBoxes = null; Image[] images = null; private void playLevel() { try

只有将游戏设置从4个边长更改为9/16/25时,才会发生此异常。提前谢谢

int LengthSides = Properties.Settings.Default.NumberOfSquares;
Image image;
PictureBox picBoxWhole = null;
PictureBox[] picBoxes = null;
Image[] images = null;

private void playLevel()
        {
            try
            {
                currentLevel = LengthSides;

                if (picBoxWhole != null)
                {
                    panel1.Controls.Remove(picBoxWhole);
                    picBoxWhole.Dispose();
                    picBoxWhole = null;
                }

                if (picBoxes == null)
                {
                    picBoxes = new PictureBox[currentLevel];
                    images = new Image[currentLevel];
                }

                int numRow = (int)Math.Sqrt(currentLevel);
                int numCol = numRow;
                int unitX = panel1.Width / numRow;
                int unitY = panel1.Height / numCol;
                int[] indice = new int[currentLevel];

                for (int i = 0; i < currentLevel; i++)
                {
                    indice[i] = i;

                    if (picBoxes[i] == null)
                    {
                        picBoxes[i] = new MyPictureBox();
                        //picBoxes[i].BorderStyle = BorderStyle.Fixed3D;
                        picBoxes[i].Click += OnPuzzleClick;
                        picBoxes[i].MouseHover += PB_MouseHover;
                        picBoxes[i].MouseLeave += PB_MouseLeave;
                        picBoxes[i].Refresh();
                    }

                    picBoxes[i].Width = unitX;
                    picBoxes[i].Height = unitY;

                    ((MyPictureBox)picBoxes[i]).Index = i;

                    createBitmapImage(image, images, i, numRow, numCol, unitX, unitY);

                    picBoxes[i].Location = new Point(unitX * (i % numCol), unitY * (i / numCol));

                    if (!panel1.Controls.Contains(picBoxes[i]))
                    {
                        panel1.Controls.Add(picBoxes[i]);
                    }
                }

                shuffle(ref indice);

                for (int i = 0; i < currentLevel; i++)
                {
                    picBoxes[i].Image = images[indice[i]];
                    ((MyPictureBox)picBoxes[i]).ImageIndex = indice[i];
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }
屏幕:

如果更改长度边,则需要更改阵列、图像框和图像的大小

现在您使用的是旧尺码

      if (picBoxes == null)
        {
            picBoxes = new PictureBox[currentLevel];
            images = new Image[currentLevel];
        }`

我希望我能正确理解这个问题。

你的问题是这个if分支

if (picBoxes == null)
{
    picBoxes = new PictureBox[currentLevel];
    images = new Image[currentLevel];
}
在初始化时,picbox为null,因此正在执行此块。PICBOX和images变量根据currentLevel的值设置宽度,该值由

currentLevel = LengthSides;
在您的问题中,您说过将游戏设置从4个边长更改为9/16/25时会出现问题

我已经在方法的名称playLevel上计算出,该方法在每个级别都被调用。因此,如果大小从4更新为9,那么

但是,你不能对Picbox做任何事情。因此,下面的if语句中出现了块

if (picBoxes == null)
{
    picBoxes = new PictureBox[currentLevel];
    images = new Image[currentLevel];
}
从来没有人打过电话。所以Picbox还是旧的,但尺寸更小:4。不是9。当然,如果您的当前级别更高,这将导致超出范围的错误。因为您正在尝试从0循环到8 i
简单的解决方案:重新初始化picbox和images字段。只需删除条件检查即可

我已经找到了答案。我在picbox=new MyPictureBox[currentLevel]之前用0初始化了所有; 图像=新图像[当前级别];初始化并从PictureBox[]类更改为MyPictureBox[]Picbox=null;现在它开始工作了

         if (picBoxes != null)
            {
                for (int i = 0; i < picBoxes.Length; i++)
                {
                    if (picBoxes[i].Image != null)
                    {
                        picBoxes[i].Image = null;
                    }

                    picBoxes[i].Index = 0;
                    picBoxes[i].ImageIndex = 0;
                    picBoxes[i].Width = 0;
                    picBoxes[i].Height = 0;
                    picBoxes[i] = null;
                }

                images = null;
            }

请粘贴完整的异常堆栈跟踪请指明在哪一行上出现错误更改长度边时是否正在初始化Picbox和图像阵列?因为,第二次调用playLevel Picbox时,它不为null,并且已经初始化。我想在你的代码中,当你改变长度边时,你应该将Picbox设置为null@Pikoh好啊我会试着回答。@Pikoh,它不起作用。我得到另一个例外,或者图像没有被分割成碎片。更改数组的大小是什么意思?这里不是变了吗:currentLevel=LengthSides?谢谢。当我删除条件或设置LengthSides值后设置Picbox=null时,Picbox不会将图像分割成碎片。
if (picBoxes == null)
{
    picBoxes = new PictureBox[currentLevel];
    images = new Image[currentLevel];
}
         if (picBoxes != null)
            {
                for (int i = 0; i < picBoxes.Length; i++)
                {
                    if (picBoxes[i].Image != null)
                    {
                        picBoxes[i].Image = null;
                    }

                    picBoxes[i].Index = 0;
                    picBoxes[i].ImageIndex = 0;
                    picBoxes[i].Width = 0;
                    picBoxes[i].Height = 0;
                    picBoxes[i] = null;
                }

                images = null;
            }