-1

How can I include the "for" loop in the name of the button?

        for (int i = 1; i <= 10; i++)
        {
            button[i].Text = "0";
        }

Is there any way to make this work? Thank you!

3
  • 1
    What's the result that you want to achieve? A button with a label that reads 0000000000? Commented Dec 25, 2013 at 13:21
  • Just to add the same text for 10 buttons efficiently. Commented Dec 25, 2013 at 13:32
  • ... and of this and this and this and this ... Commented Dec 25, 2013 at 13:35

3 Answers 3

4

Yes, you can access a button with it's name like this:

    for (int i = 1; i <= 10; i++)
    {
        string buttonName = "button" + i;
        this.Controls[buttonName].Text = "0";
    }
Sign up to request clarification or add additional context in comments.

7 Comments

I get "Object reference not set to an instance of an object." on the second line of code.
@user2992015: On {?
on "this.Controls[buttonName].Text = "0";"
Check your button names and change buttonName's value. What is your button names ? If your button names like "button1","button2" .. this code should work..you are getting that error because your button names are different
My buttons names are "button1" to "button10".
|
1

Initialize an array of Button:

Button btnsarr = new Button[ DefineTheSize ]();

and add all buttons in it:

btnarr[0] = button1;
btnarr[1] = button2;
btnarr[2] = button3;
// and so on

now you can use this array in the you want.

for (int i = 1; i <= btnarr.Length; i++)
{
    btnsarr[i].Text = "0";
}

6 Comments

You are adding same button multiple times in an array.
@RohitVats That was just a sample. Anyway ! I've corrected it.
Moreover, i think there is no need of creating an array at all. Buttons can be accessed from Controls collection like Selman22 mentioned in his answer already.
@RohitVats: Unless there are only those buttons around, the complete collection of contorls would have to be filtered, which is not a clean solution compared to an array that contains exactly the buttons that should be processed.
@RohitVats: If you put the buttons that should be included in the loop into an array once upon initialization, you can simply loop over the array. If you loop directly over the controls collection, you will have to spend some additional effort on checking which controls you actually want every time you loop over the collection.
|
0

Best way to do this is:

foreach (Button button in this.Controls.OfType<Button>().ToArray())
     button.Text = "0";

5 Comments

This will break as soon as there is any button on the form that is not supposed to be subject to the text assignment.
@O. R. Mapper I dont think so there is any such button which not support text assignment
The OP doesn't state whether or not there is any such button. Even if there isn't any right now, I'd be careful about calling something that breaks on the first tiny change to the surrounding UI as the "best way".
@O. R. Magger So how should I check this? C# provide to check so?
Instead of using this.Controls, you can add the buttons you want to treat with the loop into a list or an array, as shown in Shaharyar's answer, and then iterate over that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.