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);
}