0

did that code and the picture just dont appear no eror no message just open the screen the board there but the picture not

public static void Create(Button[,] board)
    {
        PictureBox[] BlackpawnsPictureboxes = new PictureBox[8];
        const int tileSize = 50;

        var newPictureBox = new PictureBox
        {
            Size = new Size(tileSize, tileSize),
        };

        for (var m = 0; m < 8; m++)
        {

            BlackpawnsPictureboxes[m] = newPictureBox;

            BlackpawnsPictureboxes[m].Location = board[1, m].Location;

            BlackpawnsPictureboxes[m].Image = Image.FromFile(@"C:\Users\USER\source\repos\chess\chess\piece\white\pown.jpg");

            BlackpawnsPictureboxes[m].Visible = true;

            BlackpawnsPictureboxes[m].BringToFront();
        }
    }
yuval464
  • 1
  • 4
  • You're not adding the new Controls to an existing parent container. Btw, load the Bitmap just once, assign it to a Property/Field. Maybe add it to the Project's Resources and load it from there (just once). – Jimi May 18 '20 at 11:16
  • you need something like - `Panel.Children.Add(BlackpawnsPictureboxes[m]);` – Sandris B May 18 '20 at 11:17
  • two question about that first why panel its a picture box second what do you mean by children cause it doesn't recognize it – yuval464 May 18 '20 at 11:31
  • @Jimi can you explain yourself dont really get what you said... – yuval464 May 18 '20 at 11:34
  • Controls need to be *parented* to a container. The *natural* container is the Form. All controls need to have a Form container. Most controls, when created, can also host other Controls. Use the [Controls](https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.control.controls) collection to add child Controls to an existing Control that acts as container. If you want to add your PictureBox Controls directly to the Form: `var pBox = New PictureBox(); this.Controls.Add(pBox);` or `pBox.Parent = this;`. Or add a Panel (or other container) to the Form, then `[Panel].Controls.Add(pBox);` – Jimi May 18 '20 at 11:53
  • @jimi var pBox = new PictureBox(); BlackpawnsPictureboxes[m].Controls.Add(pBox); pBox.Parent = BlackpawnsPictureboxes[m]; Panel.pBox.Add(BlackpawnsPictureboxes[m]); like that? – yuval464 May 18 '20 at 12:48
  • cause it gives me an eror "'panel' does not contain a definition for 'pBox'" – yuval464 May 18 '20 at 12:56
  • Did you add a Panel to your Form? If so, then `var pBox = new PictureBox(); [YourPanel].Controls.Add(pBox);` OR `var pBox = new PictureBox(); pBox.Parent = [YourPanel];`. The method you use depends on the context. – Jimi May 18 '20 at 13:41
  • @Jimi your panel means BlackpawnsPictureboxes[m] in my code? (the picture boxes im trying to make apear) – yuval464 May 18 '20 at 14:10
  • Nope, that's just an array of Controls you're building. If you want to add your PictureBox Controls to a Panel, go to the Form Designer and drop in a Panel from the ToolBox. – Jimi May 18 '20 at 14:12
  • @Jimi ok added a plane did that var pBox = new PictureBox(); panel1.Controls.Add(pBox); how do i continue now? – yuval464 May 18 '20 at 14:17
  • @yuval464: Even better, you could just google a bit on how to make chess in c# and you can find complete video tutorials. – D J May 19 '20 at 12:53
  • @DJ thank you but not looking for a full game just trying to make my game and challange myself for my project – yuval464 May 20 '20 at 10:43
  • @Jimi you helped me then here and I didn't understood that re-uploaded my current code can you try to reexplain that? – yuval464 May 20 '20 at 11:39

0 Answers0