0

I'm currently trying to create a nested loop system that prints out the colors from an imported csv file into multiple picture boxes. The first loop loads the picture boxes in the form into an array and the second one loops through the gameboard and the sets the color for each picture box from the csv file. I keep getting an error saying that "index is out of bounds of the array" in the array inside the second nested loop. How could I fix this? I'm using visual studio 2019 and the windows forms app (.net framework) template.

 public partial class Form1 : Form
    {
        PictureBox[,] gameBoard = new PictureBox[7, 7];

private void Form1_Load(object sender, EventArgs e)
        {
           
            string[] startingconfigarray = File.ReadAllLines(@"puzzleconfig.csv");

            int index = 1; 
            for (int i = 0; i < 7; i++)
            {
                for (int j = 0; j < 7; j++)
                {
                    gameBoard[i, j] = (PictureBox)Controls.Find("pictureBox" +
                    (index).ToString(), true)[0];
                    index++;
                }
            }

            int startingConfigIndex = 0;
            for (int i = 0; i < 7; i++)
            {
                for (int j = 0; j < 7; j++)
                {
                    gameBoard[i, j].BackColor =
                    Color.FromName(startingconfigarray[startingConfigIndex]);
                    startingConfigIndex++;
                }
            }
            

        }
    }
Atlas
  • 1
  • Have you tried [debugging](https://learn.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2019#set-a-breakpoint-and-start-the-debugger) it? It's probably at `startingconfigarray[startingConfigIndex]`, but can't tell without knowing the value of `startingconfigarray` – devNull Jul 30 '21 at 00:33
  • the code works perfectly fine in another program i used but apparently there was something wrong with the csv file so i deleted it and made a new one. It worked after that. thanks for helping – Atlas Jul 30 '21 at 02:01

0 Answers0