Afternoon all,
I have a little project that represents a petrol station. I have a user control that represents a petrol-pump. These can be added and removed dependant on how many are required.
In my user control I have a combo-box that holds the fuel choices {Diesel, Premium, Unleaded} for example.
The problem I have is that when I select a fuel in one instance of a user control, it also changes the selection in all other instances. I thought they would be completely independant of one another?
How can I achieve this? I want to have one pump running Diesel, whilst the others can be running Unleaded or Premium.
My code is below - I'm currently adding them dynamically with a click, I have however tried adding them prior to runtime in the designer, but I get the same result.
Please bear in mind that I had the same problem prior to attempting to make it asynchronous.
Kind regards,
Mike.
I have a little project that represents a petrol station. I have a user control that represents a petrol-pump. These can be added and removed dependant on how many are required.
In my user control I have a combo-box that holds the fuel choices {Diesel, Premium, Unleaded} for example.
The problem I have is that when I select a fuel in one instance of a user control, it also changes the selection in all other instances. I thought they would be completely independant of one another?
How can I achieve this? I want to have one pump running Diesel, whilst the others can be running Unleaded or Premium.
My code is below - I'm currently adding them dynamically with a click, I have however tried adding them prior to runtime in the designer, but I get the same result.
Code:
private void btnAddPump_Click(object sender, EventArgs e)
{
//creates a petrolpump control instance and assigns a pump identifier
//tlpPumpControlHolder.Controls.Add(new PetrolPump());
PetrolPump pumpInstance = new PetrolPump(_pumpIndex);
tlpPumpControlHolder.Controls.Add(pumpInstance);
//increment unique identifier
_pumpIndex++;
//needs calling to update fuel list for new pumps
UpdatePumpsWithFuels();
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace PumpingStation_RunThrough
{
public partial class PetrolPump : UserControl
{
private int pumpIndex; //is this needed?
private List<Fuel> _fuelList;
private Fuel _selectedFuel;
#region Constructors
public PetrolPump()
{
InitializeComponent();
}
public PetrolPump(int PumpIndex)
{
pumpIndex = PumpIndex;
if (InvokeRequired)
{
MethodInvoker theFunction = new MethodInvoker(PetrolPump_ASync);
Invoke(theFunction, null);
}
else
{
PetrolPump_ASync();
}
pumpIndex = PumpIndex;
//InitializeComponent();
//ThreadPool.QueueUserWorkItem(new WaitCallback(PetrolPump_ASync));
}
private void PetrolPump_ASync()
{
//pumpIndex = PumpIndex;
InitializeComponent();
}
#endregion
public List<Fuel> FuelList
{
get { return _fuelList; }
set
{
_fuelList = value;
UpdateFuelCombobox();
}
//extra stuff to go here. When set is called a method will be required to update the PetrolPumps fuel combobox
}
private void UpdateFuelCombobox()
{
//make threadsafe
if (InvokeRequired)
{
MethodInvoker theFunction = new MethodInvoker(UpdateFuelCombobox);
}
else
{
this.cmbFuelSelection.DataSource = null;
this.cmbFuelSelection.DataSource = _fuelList;
}
}
}
}
Please bear in mind that I had the same problem prior to attempting to make it asynchronous.
Kind regards,
Mike.
Last edited: