Jibrohni Posted June 4, 2010 Posted June 4, 2010 (edited) 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. 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. Edited June 5, 2010 by Jibrohni Quote
Administrators PlausiblyDamp Posted June 4, 2010 Administrators Posted June 4, 2010 Any chance you could post the code for the PetrolPump class as it is highly likely that is were the problem lies. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Jibrohni Posted June 5, 2010 Author Posted June 5, 2010 the code's there - thought I'd included it to start with!? Thank you. Quote
Administrators PlausiblyDamp Posted June 5, 2010 Administrators Posted June 5, 2010 Without having the rest of the code to look at I am guessing a bit but how are you assigning the fuel list to the pumps? Is there just a single List you are assigning to each of them? If so you can see the same problem displayed with much simpler code e.g. List test = new List(); test.Add("one"); test.Add("two"); test.Add("three"); comboBox1.DataSource = test; comboBox2.DataSource = test; will do exactly the same thing. Not sure if the following code is the cleanest solution but you could simply create an array as a copy of the source for each combobox (think it requires .Net 3 or later) List test = new List(); test.Add("one"); test.Add("two"); test.Add("three"); comboBox1.DataSource = test.ToArray(); comboBox2.DataSource = test.ToArray(); Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.