问题
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?
回答1:
You can do it the same way as for any other array. For example:
Button[] array = { firstButton, secondButton };
or if you need to declare in one place and assign later:
Button[] array;
...
array = new Button[] { firstButton, secondButton };
In C# 3+ you can use implicit typing for array initializers:
Button[] array;
...
array = new[] { firstButton, secondButton };
You might also want to consider using a List<Button> instead:
List<Button> buttons = new List<Button> { firstButton, secondButton };
回答2:
LINQ to the rescue!!
Button[] buttons = this.Controls.OfType<Button>().ToArray();
回答3:
var myButtonArray = new [] {this.Button1, this.Button2, ...}
To streamline this process if there are a lot of Buttons, you could try this code at the form level:
this.Controls.OfType<Button>().ToArray();
You could recurse this with any Control in the Controls collection that has a nonempty Controls collection itself.
回答4:
something like:
var myButtonArray = new[] {btn1, btn2, btn3, btn4};
回答5:
You have all your controls in the Controls property of your form, so you have to iterate that collection and add it to your array.
List<Button> buttons = new List<Button>();
foreach(Control c in this.Controls)
{
Button b = c as Button;
if(b != null)
{
buttons.Add(b);
}
}
回答6:
If you are using C# 7.0 or higher you can use the is keyword to check if each control is a button as you loop through them.
List<Button> buttons = new List<Button>();//CREATE LIST FOR BUTTONS
//LOOP THROUGH EACH CONTROL ON FORM
foreach (Control c in Controls)
{
//IF THE CONTROL IS A BUTTON ADD IT TO THE LIST
if (c is Button b)
{
buttons.Add(b);
}
}
For older versions of C# please see @Edgar Hernandez's answer
回答7:
Assuming there is a naming convention...
List<Button> asdf = new List<Button>();
for (int x = 0; x <= 10; x++) {
asdf.Add(myButton + x);
}
回答8:
In response to your requirements: (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?)
List<Button> buttons = new List<Button> { firstButton, secondButton };
// Iterate through the collection of Controls, or you can use your List of buttons above.
foreach (Control button in this.Controls)
{
if (button.GetType() == typeof(Button)) // Check if the control is a Button.
{
Button btn = (Button)button; // Cast the control to Button.
btn.Click += new System.EventHandler(this.button_Click); // Add event to button.
}
}
// Click event for all Buttons control.
private void button_Click(Button sender, EventArgs e)
{
ChangeForeColor(sender); // A method that accepts a Button
// other methods to do...
// you can also check here what button is being clicked
// and what action to do for that particular button.
// Ex:
//
// switch(sender.Name)
// {
// case "firstButton":
// sender.ForeColor = Color.Blue;
// break;
// case "secondButton ":
// sender.Text = "I'm Button 2";
// break;
// }
}
// Changes the ForeColor of the Button being passed.
private void ChangeForeColor(Button btn)
{
btn.ForeColor = Color.Red;
}
来源:https://stackoverflow.com/questions/4037716/create-array-collection-of-buttons-from-existing-buttons