C# 是否可以不在列表框中为每个输入添加新行?

C# 是否可以不在列表框中为每个输入添加新行?,c#,winforms,listbox,C#,Winforms,Listbox,我有一个winforms应用程序,它接收用户输入,并将其添加到列表中。然后,此列表将显示在列表框中 我的问题是,我希望列表框只在一行上显示单个项目一次。 目前看起来是这样的: Product Price Quantity Bread 1.20 1 Bread 2.40 2 Bread 3.60 3 Eggs 1.50 1 Eggs 3.00 2 我希望它能显示: Product Pr

我有一个winforms应用程序,它接收用户输入,并将其添加到列表中。然后,此列表将显示在列表框中

我的问题是,我希望列表框只在一行上显示单个项目一次。 目前看起来是这样的:

Product    Price    Quantity
Bread      1.20     1
Bread      2.40     2
Bread      3.60     3
Eggs       1.50     1
Eggs       3.00     2
我希望它能显示:

Product    Price    Quantity
Bread      3.60     3
Eggs       3.00     2
有可能做到这一点吗

private void btnAdd_Click(object sender, EventArgs e)
    {
        lbBasket.DisplayMember = "ProductName";
        try
        {
            string name = textBoxProduct.Text.ToString();
            decimal price = Convert.ToDecimal(textBoxPrice.Text);
            int quantity = Convert.ToInt32(textBoxQuantity.Text);

            if (string.IsNullOrWhiteSpace(textBoxQuantity.Text))
            {
                shoppingBasket.AddProduct(name, price);
                lbBasket.Items.Add(textBoxProduct.Text);
                lbPrice.Items.Add(shoppingBasket.BasketTotal);
                lbQuantity.Items.Add(shoppingBasket.NumberOfTotalQuantities);
            }
            else
            {
                shoppingBasket.AddProduct(name, price, quantity);
                lbPrice.Items.Add(shoppingBasket.BasketTotal);
                lbQuantity.Items.Add(shoppingBasket.NumberOfTotalQuantities);

                foreach (OrderItem item in shoppingBasket)
                {
                    if (item.ProductName == name)
                    {
                        lbBasket.Items.Add(item.ProductName);
                    }
                }
            }
        }

只需添加类似于以下内容的代码:

foreach(listboxitem itm in lbBasket)
{
  if(itm.toString()!=textBoxProduct.Text)
  {
    lbBasket.Items.Add(textBoxProduct.Text);
  }
}

同样,为您拥有的所有3个列表框保留循环。

您可以使用LINQ:

class Program
{
    static void Main(string[] args)
    {
        List<ShoppingBasket> bag = new List<ShoppingBasket>();

        bag.Add(new ShoppingBasket("Bread", 1.20M, 1));
        bag.Add(new ShoppingBasket("Bread", 2.40M, 2));
        bag.Add(new ShoppingBasket("Bread", 3.60M, 3));

        bag.Add(new ShoppingBasket("Eggs", 1.50M, 1));
        bag.Add(new ShoppingBasket("Eggs", 3.000M, 2));


        var result = from x in bag
                    group x by x.Product into g
                   select new ShoppingBasket(g.Key, g.Max(x => x.Price),
                   g.Max(x => x.Quantity));
    }
}
public class ShoppingBasket
{
    public string Product { get; set; }
    public decimal Price { get; set; }
    public int Quantity { get; set; }

    public ShoppingBasket(string product, decimal price, int quantity)
    {
        this.Product = product;
        this.Price = price;
        this.Quantity = quantity;
    }
}
类程序
{
静态void Main(字符串[]参数)
{
列表包=新列表();
添加(新购物篮(“面包”,1.20M,1));
添加(新购物篮(“面包”,2.40M,2));
添加(新购物篮(“面包”,3.60M,3));
添加(新购物篮(“鸡蛋”,1.50M,1));
添加(新购物篮(“鸡蛋”),3.000M,2);
var结果=来自袋中的x
将x按x分组。产品分为g
选择新购物篮(g.Key,g.Max(x=>x.Price),
g、 最大值(x=>x.Quantity));
}
}
公营购物篮
{
公共字符串乘积{get;set;}
公共十进制价格{get;set;}
公共整数数量{get;set;}
公共购物篮(字符串产品、十进制价格、整数数量)
{
这个。产品=产品;
这个。价格=价格;
这个。数量=数量;
}
}

下面是一个完整的示例,演示如何使用单个列表框

using System;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            lbBasket.Items.Add(new Basket("Name 1", (decimal) 1.00, 1));
            lbBasket.Items.Add(new Basket("Name 2", (decimal) 2.00, 2));
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            var newItem = new Basket(txtName.Text, Convert.ToDecimal(txtPrice.Text), Convert.ToInt32(txtQuantity.Text));

            var existingItem =
                lbBasket.Items.Cast<Basket>()
                    .FirstOrDefault(li => li.Name.Equals(newItem.Name, StringComparison.OrdinalIgnoreCase));
            // There is something there 
            if (existingItem != null)
            {
                // You already have the best one
                if (existingItem.Price > newItem.Price)
                {
                    // Do nothing
                    return;
                }
                // Price is the same
                if (existingItem.Price == newItem.Price)
                {
                    lbBasket.Items.Remove(existingItem);
                    newItem.Quantity += existingItem.Quantity;
                    lbBasket.Items.Add(newItem);
                }
                // Remove the old item and add the new one
                else if (existingItem.Price < newItem.Price)
                {
                    lbBasket.Items.Remove(existingItem);
                    lbBasket.Items.Add(newItem);
                }
            }
            else
            {
                lbBasket.Items.Add(newItem);
            }
        }
    }

    public class Basket
    {
        public string Name { get; set; }
        public decimal Price { get; set; }
        public int Quantity { get; set; }

        public Basket(string name, decimal price, int quantity)
        {
            Name = name;
            Price = price;
            Quantity = quantity;
        }

        public override string ToString()
        {
            return Name + " £" + Price + " " + Quantity;
        }
    }
}
使用系统;
使用System.Linq;
使用System.Windows.Forms;
命名空间Windows窗体应用程序1
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
lbBasket.Items.Add(新篮子(“名称1”,(十进制)1.00,1));
lbBasket.Items.Add(新篮子(“名称2”,(十进制)2.00,2));
}
私有void btnAdd_单击(对象发送者,事件参数e)
{
var newItem=newbasket(txtName.Text,Convert.ToDecimal(txtPrice.Text),Convert.ToInt32(txtQuantity.Text));
var现有项目=
lbBasket.Items.Cast()
.FirstOrDefault(li=>li.Name.Equals(newItem.Name,StringComparison.OrdinalIgnoreCase));
//那里有些东西
如果(existingItem!=null)
{
//你已经有最好的了
如果(existingItem.Price>newItem.Price)
{
//无所事事
返回;
}
//价格是一样的
if(existingItem.Price==newItem.Price)
{
lbBasket.Items.Remove(现有项);
newItem.Quantity+=现有Item.Quantity;
lbBasket.Items.Add(newItem);
}
//删除旧项目并添加新项目
else if(existingItem.Price
当您没有指定DisplayMember时,就会调用ToString,因此我在Basket类中重写了than,以显示您可能希望它的显示方式。根据您自己的需要编辑它,但是这有您需要的基本登录,应该可以帮助您进行操作。如果您有任何问题,请随时提问……:)


逻辑是,如果名称已经存在,那么它将检查价格,如果新项目中的价格相同,那么它将检查数量,如果数量更高,那么它将更新。如果价格更高,它将更新。在所有其他情况下,不会进行任何更新。

显示您如何向ListBox添加值的代码您不能对添加的项目使用字典,并保持每个唯一项目的总价格。然后在每次创建新项目时重新绑定列表added@RayB151-您似乎使用了三个不同的列表框。所以不,我不认为你能简单地实现你想要的。如果您使用的是单个列表框,则是。但是,如果您要求显示具有最高值或最高数量的项目(根据您的示例推断),则显示此数据的两个列表框之间没有实际链接。啊,好的。我认为使用三个列表框会更容易,但如果可能的话,我将只更改为一个。我将很快添加一个完整的解决方案。这看起来可能对我有用。我需要调整一些东西,因为我有一个单独的dll与方法和属性,但我认为我有正确的想法,非常感谢!没问题。如果您还有任何问题,请告诉我们!:)这一切都很好,除了我想它更新的数量和价格。现在,如果我指定数量为4,它将设置为4。(我在表单上有一个添加按钮)。如果我再按一次,没有什么变化,我希望它能读到8的数量(和相应的价格)。我能做到吗?请把我的评论投上一票,我会接受这一点作为贿赂为你做这件事;)我甚至不知道这是可能的P