Reputation: 67
I create a GroupBox
and Label
s inside this GroupBox
, so I want to know how can I access to all GroupBox
of my Form
and set Label.Text
to "0"
:
I try it as:
void clearLabels()
{
var labelList = this.Controls.OfType<GroupBox>();
foreach (var item in labelList)
{
item.Text = "0";
}
}
But it just set 0
to title of groupbox as:
is it possible to access Label
s of a GroupBox
to set them to "0"
instead Text
of GroupBox
? Regards
Upvotes: 2
Views: 37
Reputation: 186813
It seems, you want nested loops:
void clearLabels()
{
var groupBoxes = this.Controls.OfType<GroupBox>();
// GroupBoxes
foreach (var item in groupBoxes)
{
// Labels within GroupBox
foreach(var lbl in item.Controls.OfType<Label>())
{
lbl.Text = "0";
}
}
}
Upvotes: 4