User Control - Multiple of same

Jibrohni

Freshman
Joined
Apr 15, 2010
Messages
28
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.

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:
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<Fuel> you are assigning to each of them? If so you can see the same problem displayed with much simpler code e.g.

C#:
          List<string> test = new List<string>();
            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)
C#:
            List<string> test = new List<string>();
            test.Add("one");
            test.Add("two");
            test.Add("three");

            comboBox1.DataSource = test.ToArray();
            comboBox2.DataSource = test.ToArray();
 
Back
Top