2014-01-15 21 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; 
    } 
} 
相關問題