-1

我正在按照本教程制作 ac# 益智游戏,http://jetgamedev.blogspot.ro/2012/05/lesson-0229-c-lab-4-create-image-puzzle.html

我的问题是在教程的第 77 步,它找不到任何 PictureBox 类,所以我将 Form1 添加为父类。现在我在第 86 步收到此错误:

Cannot implicitly convert type 'PuzzleImage.MyPictureBox' to 'System.Windows.Forms.PictureBox'

问题出在以下几行:

picBoxes[i] = new MyPictureBox();
((MyPictureBox)picBoxes[i]).Index = i;
 ((MyPictureBox)picBoxes[i]).ImageIndex = indice[i];

这是问题源代码的一部分:

 private void PlayLevel()
     {
         if (pictureBoxWhole != null)
         {
             groupboxPuzzle.Controls.Remove(pictureBoxWhole);
             pictureBoxWhole.Dispose();
             pictureBoxWhole = null;
         }
         if (picBoxes == null)
         {
             images = new Image[currentLevel];
             picBoxes = new PictureBox[currentLevel];
         }
         int numRow = (int)Math.Sqrt(currentLevel);
         int numCol = numRow;
         int unitX = groupboxPuzzle.Width / numRow;
         int unitY = groupboxPuzzle.Height / numCol;
         int[] indice = new int[currentLevel];
         int i = 0;
         for (i = 0; i < currentLevel; i++)
         {
             indice[i] = i;
             if (picBoxes[i] == null)
             {
                 picBoxes[i] = new MyPictureBox();
                 picBoxes[i].Click += new EventHandler(OnPuzzleClick);

                 picBoxes[i].BorderStyle = BorderStyle.Fixed3D;

             }
             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 (!groupboxPuzzle.Controls.Contains(picBoxes[i]))
                 groupboxPuzzle.Controls.Add(picBoxes[i]);


         }
         suffle(ref indice);
         for (i = 0; i < currentLevel; i++)
         {
             picBoxes[i].Image = images[indice[i]];
             ((MyPictureBox)picBoxes[i]).ImageIndex = indice[i];
         }
     }

感谢您的时间。

4

1 回答 1

2

picBoxes是一个数组PictureBox。每个元素都是 type PictureBox

你有这一行:

picBoxes[i] = new MyPictureBox();

当您尝试分配不兼容的类型时。

如果您将其更改为:

picBoxes[i] = new PictureBox();

它会起作用的。

于 2013-03-21T21:09:44.240 回答