is there a simple way of creating a button collection from the existing buttons on my form? (In c#).
I have a series of buttons already on my form and I want to use an index to access them…e.g.:
myButtonArray[0].ForeColor ...// Do something with it
Can this be done?
Edit: Can I set the array to have a generic OnClick event? And then determine which button in the array was clicked and, say, change its color?
You can do it the same way as for any other array. For example:
or if you need to declare in one place and assign later:
In C# 3+ you can use implicit typing for array initializers:
You might also want to consider using a
List<Button>instead: