user1439564
user1439564

Reputation: 115

C# change multiple labels by looping

To prevent code duplication, I`m looking for a solution for this below:

    private void sensor1SetUnits(string newUnit)
    {
        foreach (Control ctrl in groupBoxSensor1.Controls)
        {
            // initialize all labels
            if (ctrl is Label)
            {
                ((Label)ctrl).Text = newUnit;
            }
        }
    }

    private void sensor2SetUnits(string newUnit)
    {
        foreach (Control ctrl in groupBoxSensor2.Controls)
        {
            // initialize all labels
            if (ctrl is Label)
            {
                ((Label)ctrl).Text = newUnit;
            }
        }
    }
  private void uiInitControls()
    {
        sensor1SetUnits(units.celsius);
        sensor2SetUnits(units.celsius);
    }

However, I've more than 10 groupboxes, and I need to change everytime all the labels to another unit.

I would expect something like this:

    private void uiSensorChangeUnits(Control * ptrCtrl)
    {
        foreach (Control ctrl in ptrCtrl)
        {
            // initialize all labels
            if (ctrl is Label)
            {
                ((Label)ctrl).Text = units.celsius;
            }
        }

    }

    private void someFunction()
    {
        uiSensorChangeUnits(&groupBoxSensor1.Controls);
        uiSensorChangeUnits(&groupBoxSensor2.Controls);
    }

Upvotes: 0

Views: 2259

Answers (3)

Matthias Müller
Matthias Müller

Reputation: 3483

Good guy LINQ is your best bet here.

    private void Form1_Load(object sender, EventArgs e)
    {
        UpdateChildrenInGroupBox<Label>("Test Label");
        UpdateChildrenInGroupBox<TextBox>("Test Textbox");

        //Wont compile
        //UpdateChildrenInGroupBox<Rectangle>("Test Rectangle");
    }

    private void UpdateChildrenInGroupBox<T>(string value) where T: Control
    {
        var allGBs = this.Controls.OfType<GroupBox>();
        var allControlsInGBs = allGBs.SelectMany(f => f.Controls.Cast<Control>());
        allControlsInGBs.OfType<T>().ToList().ForEach(f => f.Text = value);
    }

We loop trough all Controls at the Form,which has the Type GroupBox. Then we select the Controls and right cast it to them. Then we get the passed Type and set the Value of them.

Upvotes: 1

Steve
Steve

Reputation: 216358

You can pass a GroupBox to a method, then looking for the appropriate control could be reduced with the OfType extension

private void SetSensorUnitLabels(GroupBox currentGB, string newUnit)
{
    foreach (Label ctrl in currentGB.Controls.OfType<Label>())
    {
       ctrl.Text = newUnit;
    }
}


SetSensorUnitLabels(groupBoxSensor1, units.celsius);
SetSensorUnitLabels(groupBoxSensor2, units.celsius);

Upvotes: 2

Guilherme Duarte
Guilherme Duarte

Reputation: 3441

private void UpdateUnitLabels(GroupBox groupBox, string unit)
{
    foreach (Control control in groupBox.Controls)
    {
        var label = control as Label;
        if (Label != null)
        {
            label.Text = unit;
        }
    }
}

Upvotes: 0

Related Questions