How to rename multiple buttons in one loop in C#
I have a battleship-like program where there is a 10 x 10 grid of buttons. In the beginning of the program, i would like all of the buttons' text to be changed to "---", which shows that no one has shot at that coordinate. I can't seem to find a way to rename all of the buttons in one loop. The buttons all have names b00, b01, b02... which show their coord开发者_如何转开发inates. the first number is the row and the second number is the column. (i.e. b69 represents row 7, column 10).
Hope you can help!
Thank you in advance!
Luke
You can also use Extension Method OfType() to filtering based on specified type.See the next example
foreach (Button btn in this.Controls.OfType<Button>())
{
btn.Text="New Name";
}
as you can see by using OfType extension method you do not need to casting the control to Button Type
Hope that help.
Regards
How about this:
foreach (Control c in this.Controls) {
if (c is Button) {
((Button)c).Text = "---";
}
}
This snippet loops through all controls on the form (this
), check if each is a Button and if you set its Text property to "---". Alternately, if your buttons were on some other container, such as a Panel you'd replace this.Controls
with yourPanel.Controls
.
You could get the list of controls from the parent container and loop through them.
Something like this:
foreach (Control c in myForm.Controls)
{
if (c is Button)
{
((Button)c).Text = "---";
}
}
Consider adding each of the buttons to a list:
// instantiate a list (at the form class level)
List<Button> buttonList = new List<Button>();
// add each button to the list (put this in form load method)
buttonList.add(b00);
buttonList.add(b01);
... etc ...
Then you can set a given button like this:
buttonList(0).Text = "---" // sets b00
Or all of the button thus:
foreach (Button b in buttonList) {
b.Text = "---";
}
Other possibilities abound:
Putting the buttons in a 2D array to allow addressing by row and column. You could still do a foreach over the array to set all at once.
Creating the buttons (and setting size and loction) progamatically to avoid having to create all the buttons in the designer. This would also allow you to set the grid size at run-time.
What I do in situations like this, is to store the controls that I want to manipulate frequently into a List
or preferably an IEnumerable<>
collection, which I normally initialize at construction or in the Load event handler of the containing control (e.g., if these controls are contained in a Form
, or are enclosed in a GroupBox
). By doing this, I hope to reduce the hit of having to find these controls every time I need this collection. If you only need to do this once, I wouldn't bother adding the buttons
collection.
So, something like this:
// the collection of objects that I need to operate on,
// in your case, the buttons
// only needed if you need to use the list more than once in your program
private readonly IEnumerable<Button> buttons;
In the constructor or load event handler:
this.buttons = this.Controls.OfType<Button>();
And then, whenever I need to update these controls, I just use this collection:
foreach (var button in this.buttons)
{
button.Text = @"---";
// may wanna check the name of the button matches the pattern
// you expect, if the collection contains other
// buttons you don't wanna change
}
foreach(Control c in this.Controls)
{
if(c.type==new Button().type)
{
Button b=(Button)c;
b.Text="";
}
}
精彩评论