-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!

user2992015
  • 53
  • 1
  • 4
  • 1
    What's the result that you want to achieve? A button with a label that reads `0000000000`? – Sergey Kalinichenko Dec 25 '13 at 13:21
  • Just to add the same text for 10 buttons efficiently. – user2992015 Dec 25 '13 at 13:32
  • ... and of [this](http://stackoverflow.com/questions/12055648/converting-string-to-a-control-name-in-c-sharp) and [this](http://stackoverflow.com/questions/18248989/how-to-concat-variable-integer-in-control-name-in-vb-net) and [this](http://stackoverflow.com/questions/3459230/dynamic-variable-name-use-in-c-sharp-for-winforms) and [this](http://stackoverflow.com/questions/14578957/equivalent-to-refer-to-control-by-variable-name) ... – O. R. Mapper Dec 25 '13 at 13:35

3 Answers3

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";
    }
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
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";
}
Shaharyar
  • 12,254
  • 4
  • 46
  • 66
  • You are adding same button multiple times in an array. – Rohit Vats Dec 25 '13 at 13:25
  • @RohitVats That was just a sample. Anyway ! I've corrected it. – Shaharyar Dec 25 '13 at 13:25
  • Moreover, i think there is no need of creating an array at all. Buttons can be accessed from `Controls collection` like [Selman22](http://stackoverflow.com/a/20773234/632337) mentioned in his answer already. – Rohit Vats Dec 25 '13 at 13:27
  • @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. – O. R. Mapper Dec 25 '13 at 13:38
  • @O.R.Mapper - Sorry mate didn't get you. All collection will be filtered. What? – Rohit Vats Dec 25 '13 at 13:46
  • 1
    @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. – O. R. Mapper Dec 25 '13 at 13:47
0

Best way to do this is:

foreach (Button button in this.Controls.OfType<Button>().ToArray())
     button.Text = "0";
Ankush Madankar
  • 3,689
  • 4
  • 40
  • 74
  • 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 Dec 25 '13 at 13:45
  • @O. R. Mapper I dont think so there is any such button which not support text assignment – Ankush Madankar Dec 25 '13 at 13:59
  • 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. Mapper Dec 25 '13 at 14:04
  • @O. R. Magger So how should I check this? C# provide to check so? – Ankush Madankar Dec 25 '13 at 14:32
  • 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](http://stackoverflow.com/users/1910920/shaharyar) answer, and then iterate over that. – O. R. Mapper Dec 25 '13 at 14:59