益智游戏指数超出范围异常

时间:2017-05-22 08:37:46

标签: c# windows puzzle

仅在将游戏设置从4 LengthSides更改为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);
            }
        }

错误截图: enter image description here

更新 我可以分享这个项目来测试和修复这个bug。当从较大的值更改为小于它的值(例如25 - 4)时,否则会抛出此异常。

private void switchImages(MyPictureBox box1, MyPictureBox box2)
        {
            int tmp = box2.ImageIndex;
            box2.Image = images[box1.ImageIndex];
            box2.ImageIndex = box1.ImageIndex;
            box1.Image = images[tmp];
            box1.ImageIndex = tmp;

            if (isSuccessful())
            {
                stopAllTimers();
                firstBox.BorderStyle = BorderStyle.None;
                secondBox.BorderStyle = BorderStyle.None;
                box1.BorderStyle = BorderStyle.None;
                box2.BorderStyle = BorderStyle.None;
                Properties.Settings.Default.LevelCompleted = true;
            }
        }

屏幕: enter image description here

3 个答案:

答案 0 :(得分:0)

如果您更改LengthSides,则需要更改数组picBoxesimages的大小。

现在您使用的是旧尺寸

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

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

答案 1 :(得分:0)

你的问题是这个if-branch

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

在init,picBoxesnull,因此正在执行此块。变量picBoxesimages设置为宽度,基于currentLevel的值,然后由

定义
currentLevel = LengthSides;

在您的问题中,您已经说过问题发生在&#34; 将游戏设置从4个长度更改为9/16/25 &#34;。

我已经知道方法的名称playLevel(),在每个级别调用此方法。因此,如果尺寸从4更新为9,那么

currentLevel = LengthSides; // becomes 9

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

if (picBoxes == null)
{
    picBoxes = new PictureBox[currentLevel];
    images = new Image[currentLevel];
}
永远不会打电话给

。因此,picBoxes仍处于旧状态,但规模较小:4。不是9.当然,如果currentLevel更高,则会导致超出范围错误。因为您尝试从0到8(i < currentLevel)循环,而picBoxes的长度到目前为止最长为4。

简单解决方案:重新初始化picBoxes images字段。 (只需删除条件检查)

答案 2 :(得分:0)

我已经弄清楚了。我在picBoxes = new MyPictureBox[currentLevel]; images = new Image[currentLevel];初始化之前初始化为0,并且从PictureBox[]类更改为MyPictureBox[] picBoxes = 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;
            }