Member Avatar for Moirae

Hello, I have this problem with multidimensional array on Form.

I have multidimensional array (like 5x5 matrix), but I can't add buttons on the form, if I use Controls.Add it adds only one button, and if i use Controls.AddRange program gives me an error

I want to make a logical game and multidimensional array is the key to my idea. This is the code I used, the commented lines are the ones that didn't work.

private void Form1_Load(object sender, EventArgs e)
        {
            Button[,] btnArr = new Button[5, 5];
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    btnArr[i, j] = new Button();
                    btnArr[i, j].Size = new Size(75, 75);
                    btnArr[i, j].Text = "Button" + ((i + 1) * (j + 1));
                    //Controls.AddRange(btnArr[i,j]);
                    //btnArr[i, j].Controls.Add(Button);
                }
               
            }

           
        }

Thanks for helping me :)

EDIT: I tried solving it with this code also

foreach (Button element in poljeButtona[Konstante.RETCI, Konstante.STUPCI])
                Controls.Add(element);
Member Avatar for Moirae

And I also did something like this:

private void Form1_Load(object sender, EventArgs e)
        {
            Button[,] btnArr= new Button[5, 5];
            Button b;
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    b = new Button();
                    b.Size = new Size(75, 75);
                    b.Text = "Button" + ((i + 1) * (j + 1));
                    btnArr[i, j] = b;
                    Controls.Add(b);
                   
                }
                
            }
            
        }

But also it generates only one button

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.