Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (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 by Jibrohni
  • Administrators
Posted

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

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...