我正在按照本教程制作 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];
}
}
感谢您的时间。