'c# how to add Button to array and set text of all buttons

Hi my problem is i want to add buttons to array size: 65 and there is a problem here's my code:

Button[] buttonsa = new Button[65];
for (int d = 0; d <= buttonsa.Length; d++)
                {
                    try
                    {
                        buttonsa[d].Text = "Runned!";
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }

My all code for creating button to scene:

private void CreateButton(int xh, int yh, int width, int height)
        {
            Button newButton = new Button();
            if (width < 100)
            {
                newButton.Width = 100;
            }
            else
            {
                newButton.Width = width;
            }
            if (height < 30)
            {
                newButton.Height = 30;
            }
            else
            {
                newButton.Height = height;
            }
            
            
            newButton.Text = "button" + btnIndex;
            newButton.ForeColor = Color.FromArgb(35,35,35,0);
            newButton.Name = "btn" + btnIndex.ToString();
            newButton.AccessibleName = btnIndex.ToString() + "|FlatStyle=" + FlatStyle.System.ToString() + "|Pussy=sussy";
            newButton.ContextMenuStrip = btnContext;
            newButton.Tag = "button" + btnIndex;
            newButton.FlatStyle = FlatStyle.Flat;
            newButton.BackColor = Color.White;
            newButton.Location = new Point(xh, yh);
            newButton.Click += newButton_Click2;
            buttonsa[btnIndex] = newButton;
            btnIndex++;


            int x = rand.Next(10, pic.ClientSize.Width - newButton.Width);
            int y = rand.Next(10, pic.ClientSize.Height - newButton.Height);
            newButton.Location = new Point(xh, yh);
            pic.Controls.Add(newButton);
            buttonsInScene.Add(newButton);
            this.CenterToScreen();
            g.Clear(Color.FromArgb(35, 35, 35, 0));
            pic.Refresh();
        }

Im working on big project if anyone can help me Thanks alot! <3

I tried a few times to fix it i needed to get all my buttons in form and set text to "Runned!"



Solution 1:[1]

You can use the recursive method for getting all buttons on the form. You don't need to add all buttons to the array type, anytime you can use your function and gets all buttons on the form and on the child controls. But, if you want, you can write easy code inside the function, for adding buttons to an array or to the List.

Example:

 private void ButtonList (Control cont)
    {
        foreach (Control c in cont.Controls)
        {
            if (c is Button) 
            {
                (c as Button).Text = "Hello";
                (c as Button).Visible = true;
                // and etc... 
                /*************** example adding button object to list *************
                   declare listButton property of type List<Button> on your code 
                   listButton.Add((c as Button));
                *******************************************************************/
            }
            if (c.Controls.Count > 0)
            {
                ButtonList(c);
            }
        }
    }

And you can use this function calling that

ButtonList (MainForm);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ramin Faracov