Reputation: 117
i'm writing a program in C# , which contains a dozen toolstrips and each one contains a toolstriplabel :
toolstriplabel1 toolstriplabel2 toolstriplabel3 . . . toolstriplabel11 toolstriplabel12
i want to change each ones text with a "for" loop , how can i do that?? i can use "if" with other variables to meet this , but i want to avoid the dozen "if"s i have to write. How Can i use a "for" loop with toolstriplabel's , text property? i tried using this code , it doesn't work :
for (int r = 0; r < NumGraphs; r++)
{
toolStripLabel+"i".text=...
}
Upvotes: 0
Views: 56
Reputation: 12626
What about using LINQ on collection of controls?
var labels = this.Controls.Cast<Control>()
.OfType<ToolStripLabel>()
.Where(l => l.Name.Contains("toolstriplabel"));
Then you can simply loop through them with a foreach loop.
foreach (var label in labels) { label.Text = ""; }
Upvotes: 0
Reputation: 4293
You can simply try something like this,
foreach (Control ctr in this.Controls)
{
if (ctr is ToolStripLabel)
{
// ur code
}
}
Upvotes: 1
Reputation: 5110
Not familiar with C# but You can create an array of these toolStrips. In for
loop, set the text
property of the i
th element in the array at i
th iteration.
for (int i = 0; i < NumGraphs; i++) {
toolStripLabel[i].text="YOUR TEXT";
}
Upvotes: 0