OK! Problem resolved. Not solved, you'll note, as I'm not sure why your code was misbehaving. However, if you make these changes it works as desired.
First your
btnAddPump_Click
method. At the end you Update all existing pumps, rather than just setting the fuel for the new one. This will slow your app down considerably and I would therefore suggest the following changes.
private void btnAddPump_Click(object sender, EventArgs e)
{
PetrolPump pumpInstance = new PetrolPump(_pumpIndex);
pumpInstance.FuelList = NewFuelList();
tlpPumpControlHolder.Controls.Add(pumpInstance);
_pumpIndex++;
}
If you still want to call
UpdatePumpsWithFuels()
you can do it that way and ignore this suggestion (but see modified UpdatePumpsWithFuels() later).
Now the
NewFuelList()
method.
private List<Fuel> NewFuelList()
{
List<Fuel> result = new List<Fuel>();
foreach (Fuel fuel in _fuelList)
{
result.Add(new Fuel()
{
FuelName = fuel.FuelName,
FuelCost = fuel.FuelCost
});
}
return result;
}
The modified
UpdatePumpsWithFuels()
method.
private void UpdatePumpsWithFuels()
{
foreach (Control _control in tlpPumpControlHolder.Controls)
{
PetrolPump _pump = _control as PetrolPump;
if (_pump != null)
_pump.FuelList = NewFuelList();
}
}
I have not yet worked out why the old code misbehaved but if I do, I will let you know. For now this works.