If I have 9 buttons called button1, button2 etc up to button 9

how can I change their properties using a loop? Or can I group them together?

I have something like this

for (int i = 1; i <= 9; i++)
            {
                button.Enabled = true;
                button(i).Text = "BLAH!";
            }

where If i could (i) would be the button number at the end

I dont think I am using the word "object" in the right way, I mean by it, a name of a button made in Visual C#

Can I parse a string into an object?
Then I could make an array and cycle through that...

this may seem obvious to you but I am learning c# atm
Thanks for the help

Recommended Answers

All 4 Replies

Start with making an array of buttons:

Button[] MyButtons = new Button[9];

        private void InitButtons()
        {
            for (int i = 0; i < 9; i++)
            {
                MyButtons[i] = new Button();
                MyButtons[i].Name = "button" + i.ToString();
                MyButtons[i].Location = new Point(50 + 10 * i, 50);
                // MyButtons[i].Size....
                // MyButtons[i].Text etc.
            }
        }

Don't forget to add the array to the controlscollection of the form.

Start with making an array of buttons:

Button[] MyButtons = new Button[9];

        private void InitButtons()
        {
            for (int i = 0; i < 9; i++)
            {
                MyButtons[i] = new Button();
                MyButtons[i].Name = "button" + i.ToString();
                MyButtons[i].Location = new Point(50 + 10 * i, 50);
                // MyButtons[i].Size....
                // MyButtons[i].Text etc.
            }
        }

Don't forget to add the array to the controlscollection of the form.

I have built the form on the form editor
Its more changing the properties of the buttons (more than 1 time during the program), I already have the buttons made
thanks anyhow

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.