Question

I am following this tutorial to make a c# puzzle game, http://jetgamedev.blogspot.ro/2012/05/lesson-0229-c-lab-4-create-image-puzzle.html.

My problem is at step 77 of the tutorial, it can't find any PictureBox class so I've added Form1 as parent class. Now I am at step 86 where I get this error:

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

the problem is at the following lines :

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

here is a part of the problem source code:

 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];
         }
     }

Thank you for your time.

Was it helpful?

Solution

picBoxes is an array of PictureBox. Each element is of type PictureBox.

You have this line:

picBoxes[i] = new MyPictureBox();

When you are trying to assign an incompatible type.

If you change it to:

picBoxes[i] = new PictureBox();

It will work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top