Pepe
Pepe

Reputation: 67

Reset value of label created programatically inside a groupbox

I create a GroupBox and Labels 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 Labels of a GroupBox to set them to "0" instead Text of GroupBox? Regards

Upvotes: 2

Views: 37

Answers (1)

Dmitrii Bychenko
Dmitrii Bychenko

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

Related Questions