0
votes

I have a Listbox which adds an item that the user wishes to purchase. Each item contains a price. When a user adds an item to cart, the program adds the price to the total paymet. When the user removes item from cart, the program subtracts the price of the item from the cart. I have boot items of $15 and $10. When a list item is added, the name and price of boot is displayed in string format.

eg. cart.Items.Add("boot1 costs" + $10);

However, I would like link numerical values with each items purchased. So that it adds the price to the total payment to generate a receipt in the end.

1
Create a class for your purchased items containing Name, Price and override its ToString() method to return desired string format, Then when you want to add an item to ListBox and an instance of this class. Or as an alternative use DataTable. - Reza Aghaei
By the way, you have not accepted or voted any answer for your previous questions. Please spend some time to verify answers. You need to take a Tour. - Reza Aghaei

1 Answers

1
votes

Create model for cart item.

class CartItem
{
    public int price { get; set; }
    public string displayString{ get; set; }

}

Add items to cart

cart.Items.Add(new CartItem(){ displayString="boot1 costs" + priceVar,price=priceVar});
//specify display member and value member of cart

cart.DisplayMember = "displayString";
cart.ValueMember = "price";

you can access it by

foreach(CartItem ci in cart.Items)
{
     int itemPrice= ci.price;
     string itemText=ci.displayString;
}