2014-01-15 20 views
1

我有一个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); 
        } 
       } 
      } 
     } 
+0

显示代码如何将值添加到列表框 –

+1

您可以不使用字典中添加的项目保持每个独特项目的价格的运行总计。然后重新绑定列表,每次添加新项目 – owen79

+0

@ RayB151 - 您似乎在使用三个不同的列表框。所以不,我不认为你可以简单地达到你想要的。如果你使用单个列表框,那么是的。但是,如果您要求显示具有最高值或最高数量的项目(从您的示例推断),则显示此数据的两个列表框之间不存在实际的联系。 – Faraday

回答

1

这里是你展示了如何使用一个列表框一个完整的例子。

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

当你不指定DisplayMember然后调用toString被调用,所以我已经覆盖比篮类来显示你可能会喜欢它显示。根据自己的需要编辑它,但是这有你需要的基本登录信息,并且应该可以帮助你。如果您有任何问题随时问... :)

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

+0

这看起来可能适合我。我需要调整一些东西,因为我有一个单独的DLL的方法和属性,但我认为我有正确的想法,非常感谢! – RayB151

+1

没问题。如果您还有其他问题,请告诉我们! :) – Faraday

+0

这一切工作正常,除了我想更新数量和价格。 现在,如果我指定数量为4,它会将其设置为4.(我在表单上有一个添加按钮)。如果我再次按下它,没有任何变化,因为我希望它读取数量为8(和价格相应)。 我能做到吗? – RayB151

0

只需添加类似的代码如下:

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

同样保持环为您在所有3个列表框。

0

你可以用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; 
    } 
} 
相关问题