Items are added to a listbox as well as the number of fillings. New items added makes the number of fillings for the other items the same

I have created kisok program in C#, Windows Forms that adds a sandwich name, price, and number of selected fillings to a listbox when that specific sandwiches picturebox has been clicked on. The problem im having is that for example, if the user selects 2 fillings for a cheese sandwich and adds it to the shopping cart, then chooses the cheese sandwich again but this time with 3 fillings, the number of fillings for the first added sandwich (within the listbox) will also be changed to having 3 fillings. (The fillings are added to each sandwich by selecting fillings from a textbox and then adding this to a separate listbox next to the sandwich image. The number of fillings for each sandwich is then calculated by assigning the number of items in the listbox to an objects parameter variable called fillingsAmount.)

This is the click event handler for the cheese sandwich button, which adds the sandwich name, price and amount of fillings to a listbox called shopping cart:

private void picBoxCheese_Click(object sender, EventArgs e)  

{  

    btnRemove.Enabled = true;  
    btnCheckout.Enabled = true;

    sandwiches[(int)sandwichNames.Cheese].fillingAmount = lstbCheeseDesc.Items.Count - 1;  
    cart.AddToCart(sandwiches[(int)sandwichNames.Cheese]);    

    lblTotal.Text = "Total: " + cart.Total().ToString();  


    ShoppingCartBindingSource.ResetBindings(false);  
}

This works but whenever the same item is clicked and added to the shopping cart, however many fillings that is selected for that newly added item is the amount of fillings that is displayed for the other added items. For example i can select a cheese sandwich with 3 fillings and a ham sandwich with 0 fillings, which will work fine and display “Cheese Sandwich + 3 toppings” and “Ham Sandwich + 0 toppings” in the shopping cart. If i was to then click on the Cheese sandwich again but this time with 1 topping, the shopping cart would display “Cheese Sandwich + 1 toppings”, “Ham Sandwich + 0 toppings”, and “Cheese Sandwich + 1 toppings”.

This is accessing the variable parameter of the object called sandwiches: sandwiches[(int)sandwichNames.Cheese].fillingAmount;

This is accessing the items in the listbox that displays fillings that are selected in a checkbox:
lstbCheeseDesc.Items.Count - 1;

This is a method of a Cart class, which adds the variable parameters of the object called sandwiches. Which includes the name, price and filligns amount:
cart.AddToCart(sandwiches[(int)sandwichNames.Cheese]);

This is the object called sandwiches:
sandwiches = new List<Sandwich> { new Sandwich(1, "Cheese Sandwich", 2.99m, Image.FromFile("CheeseSandwich.jpg"), new string[]{"Extra cheese", "Ketchup", "Mayo", "Onion"}, new decimal[]{0.50m, 0.20m, 0.20m, 0.50m}, 0),<br/> new Sandwich(2, "Tuna Sandwich", 4.00m, Image.FromFile("TunaSandwich.jpg"), new string[]{"Extra cheese", "Ketchup", "mayo"}, new decimal[]{0.50m, 0.20m, 0.20m, 0.50m}, 0), new Sandwich(3, "Ham Sandwich", 3.50m, Image.FromFile("HamSandwich.jpg"), new string[]{"Extra cheese", "Ketchup", "mayo"}, new decimal[]{0.50m, 0.20m, 0.20m, 0.50m}, 0), new Sandwich(4, "Steak Sandwich", 5.00m, Image.FromFile("SteakSandwich.jpg"), new string[]{"Extra cheese", "Ketchup", "mayo"}, new decimal[]{0.50m, 0.20m, 0.20m, 0.50m}, 0) };

/************************************************************************************************/

This binds the products to the listbox called shopping cart

BindingSource ShoppingCartBindingSource = new BindingSource();  


ShoppingCartBindingSource.DataSource = cart.GetAllProducts();  
lstCart.DataSource = ShoppingCartBindingSource;  
lstCart.DisplayMember = ToString(); 

Code explanation:
The “cart.GetAllProducts();” is a method from the Cart class. This is the method:

public List<Product> GetAllProducts() { return products; }
This returns all products added to it through the following method, also in the Cart class:

public void AddToCart(Product product) { products.Add(product); }

The ToString() method is from the Sandwich Class:
public override string ToString() { return $"{name}: {price.ToString()} + {fillingAmount.ToString()} fillings"; }

This is the properties and constructor in the Sandwich class, which gets the parameters of a object to use in a ToString() method, also in the Sandwich class:

Sandwich class

decimal[] FillingPrice { get; set; }

public int fillingAmount { get; set; }

public Sandwich(int _id, string _name, decimal _price, Image _image, string[] _filling,
decimal[] _fillingPrice, int _fillingAmount)
    : base(_id, _name, _price, _image)
{
    Filling = _filling;
    FillingPrice = _fillingPrice;
    fillingAmount = _fillingAmount;
}

public override string ToString()
{
    return $"{name}:  {price.ToString()} + {fillingAmount.ToString()} fillings";
}

New contributor

Dan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật