简体   繁体   English

为列表框中的每个项目添加价格 - C# .Net、Windows 窗体应用程序

[英]Adding a price to each item in a listbox - C# .Net, Windows Forms Applications

I am trying to build an windows forms application with c#.我正在尝试使用 c# 构建 Windows 窗体应用程序。 The main functionality of the application is that it is going to allow the user to add items to a shopping cart (an listbox probably) and sum up (to a Label Text) the total price for the products added to this cart.该应用程序的主要功能是允许用户将商品添加到购物车(可能是列表框)并汇总(到标签文本)添加到该购物车的产品的总价格。 Also, a product can be added more than once.此外,可以多次添加产品。 So basically it can be called a shopping cart app.所以基本上它可以被称为购物车应用程序。

My question here is, how can I calculate the total price of items added to the shopping cart?我的问题是,如何计算添加到购物车中的商品的总价? Because, first of all, I have added all the products to this listbox as a list of strings (with Listbox.Items.Add("Product1") . So I don't know how to add a price for each product (string element of this list), and later be able to add it up.因为,首先,我已将所有产品作为字符串列表添加到此列表框(使用Listbox.Items.Add("Product1") 。所以我不知道如何为每个产品添加价格(字符串元素)这个列表),然后可以添加它。

Should I somehow add the products as objects of type Product Class (with 2 properties, name and price)?我应该以某种方式将产品添加为Product Class类型的对象(具有 2 个属性、名称和价格)? If so, how do I pass just the name property to the list of listbox items?如果是这样,我如何仅将 name 属性传递给列表框项列表?

So far, here is some of my main code:到目前为止,这是我的一些主要代码:

public Form1()
{
    InitializeComponent();
    Product products = new Product();
    AddProducts(); 
}

List<string> productList = new List<string>();

public void AddProducts()
{
    productNames.Add("Product X");
    productNames.Add("Product Z");

    foreach (var product in productList) 
    {
        ProductListBox.Items.Add(product);
    }
}

The first thing to do is to define the Product class with two separate properties, one for the Name and one for the Price .首先要做的是定义具有两个单独属性的 Product 类,一个用于Name ,一个用于Price
Then override the ToString() method to return in a single string both the Name and the Price converted to a string or just the Name.然后覆盖ToString()方法以在单个字符串中返回名称和价格转换为字符串或仅返回名称。

This override is required if you want to display the Products names and their price in a single line displayed in the ListBox.如果要在 ListBox 中显示的一行中显示产品名称及其价格,则需要此覆盖。 Indeed, the ListBox control, when bound through a DataSource, looks at the ToString method of the bound class to represent its items.实际上,ListBox 控件在通过 DataSource 绑定时,会查看绑定类的 ToString 方法来表示其项目。

public class Product
{ 
    public string Name { get; set; }
    public decimal Price { get; set; }

    // This returns both Name and Price, but, of course, if you want only
    // the name just return $"{Name}"
    public override string ToString()
    {
        return $"{Name} - {Price.ToString("C")}";
    }
}

Now, in your form constructor, you could create the list of products (statically in this example) and pass that list to the ListBox's DataSource property.现在,在您的表单构造函数中,您可以创建产品列表(在本例中是静态的)并将该列表传递给 ListBox 的 DataSource 属性。
Finally the task to compute the total is just a simple Linq call to the Sum extension and pass the result to the Label text property.最后,计算总数的任务只是对Sum扩展的简单 Linq 调用,并将结果传递给 Label 文本属性。

List<Product> products = new List<Product>();
public Form1()
{
    InitializeComponent();
    AddProducts(); 
    ProductListBox.DataSource = products;
    labelTotalPrice.Text = products.Sum(x => x.Price).ToString("C");
}

public void AddProducts()
{
    products.Add(new Product {Name = "Product X", Price = 10.4m});
    products.Add(new Product {Name = "Product Z", Price = 1.5m});
    products.Add(new Product {Name = "Product K", Price = 7.32m});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM