我正在按照本教程制作一个c#益智游戏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];
}
}
感谢您的时间。
答案 0 :(得分:1)
picBoxes
是PictureBox
的数组。每个元素都是PictureBox
类型。
你有这一行:
picBoxes[i] = new MyPictureBox();
当您尝试分配不兼容的类型时。
如果您将其更改为:
picBoxes[i] = new PictureBox();
它会起作用。