-1

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.

user2194683
  • 105
  • 2
  • 2
  • 9
  • What is `MyPictureBox`? What does it inherit from? – Oded Mar 21 '13 at 21:10
  • It is a class made at step 76. – user2194683 Mar 21 '13 at 21:12
  • 2
    Regarding "My problem is at step 77 of the tutorial, it can't find any PictureBox class so I've added Form1 as parent class." It's not going to work if you've made `MyPictureBox` inherit from `Form1`. Add `include System.Windows.Forms` and it should find `PictureBox` to inherit from. – Matthew Strawbridge Mar 21 '13 at 21:17

1 Answers1

2

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.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • Yes but at step 86 it says to do this: Goto Form1.cs, go into PlayLevel function, use MyPictureBox to replace PictureBox – user2194683 Mar 21 '13 at 21:10
  • 1
    Step 77 is `class MyPictureBox : PictureBox`. I think the OP is trying to say that didn't work. My guess is the class needs a reference to the Forms namespace. – LarsTech Mar 21 '13 at 21:11
  • @LarsTech - It may not be explicit, but I suspect the author meant to change _all_ occurrences of the `PictureBox` declaration to `MyPictureBox`. – Oded Mar 21 '13 at 21:14
  • yes but when I do that I get: the annot implicitly convert type 'PuzzleImage.MyPictureBox' to 'System.Windows.Forms.PictureBox' My actualy question is if weather should I change that portion of code or not. – user2194683 Mar 21 '13 at 21:16
  • 1
    @user2194683 - Are you certain? I suspect you didn't change `PictureBox[] picBoxes ...` to `MyPictureBox[] picBoxes ...` – Oded Mar 21 '13 at 21:17
  • @Oded thank you, that worked but got me another 3 errors `'PuzzleImage.MyPictureBox' does not contain a definition for 'BorderStyle' and no extension method 'BorderStyle' accepting a first argument of type 'PuzzleImage.MyPictureBox' could be found (are you missing a using directive or an assembly reference?)` at this part of the code `'picBoxes[i].BorderStyle = BorderStyle.Fixed3D;'` – user2194683 Mar 21 '13 at 21:24
  • @user2194683 - Are the classes in the same namespace? – Oded Mar 21 '13 at 21:26
  • @Oden Yes they are both in the PuzzleImage namespace. – user2194683 Mar 21 '13 at 21:27
  • @user2194683 - Where are you using `BorderStyle` then? The errors should point you to the lines that cause the errors, so you can fix them. I don't see these in your posted code. – Oded Mar 21 '13 at 21:29
  • @Oden these are the problem lines: `picBoxes[i].BorderStyle = BorderStyle.Fixed3D;` and `((MyPictureBox)sender).BorderStyle = BorderStyle.FixedSingle;` and `picBoxes[i].Image = images[indice[i]]; ` – user2194683 Mar 21 '13 at 21:36
  • @user2194683 What is MyPictureBox currently inheriting from in your code? – LarsTech Mar 21 '13 at 21:41
  • @user2194683 - The tutorial shows it should be `class MyPictureBox : PictureBox`. – Oded Mar 21 '13 at 21:43
  • @user2194683 Go back to Step 77. Inherit from `System.Windows.Forms.PictureBox` – LarsTech Mar 21 '13 at 21:43
  • @Oded Yes but I don't have any class called PictureBox. PictureBox appears only at that step from nowhere. – user2194683 Mar 21 '13 at 21:46
  • @user2194683 - it is a built in class. If you skipped that bit of the tutorial, why are you surprised the next bits don't function properly? – Oded Mar 21 '13 at 21:46
  • @Oded when I add inherit PictureBox I get The type or namespace name 'PictureBox' could not be found (are you missing a using directive or an assembly reference?) – user2194683 Mar 21 '13 at 21:48
  • @user2194683 - You need to include the namespace it lives in - `System.Windows.Forms`. – Oded Mar 21 '13 at 21:50
  • @Oded Oh, I didn't know that, in the tutorial it says Import the NameSpace and it shows a picture of the code without the namespace. Thank you very much. – user2194683 Mar 21 '13 at 21:53