Adding numeric values to each listbox item

568 views Asked by At

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

There are 1 answers

0
Mehul Patel On

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;
}