2013-04-29 88 views
3

我的問題如下:我有兩個ListBoxe s。首先,我存儲可訂購的產品,其次是客戶的購物車。因此,產品具有數量屬性,並且我有NumericUpDown,客戶可以在其中更改訂購產品的數量。將列表框中的對象添加到另一個

private void button2_Click(object sender, EventArgs e) //puts selectedproduct to the cart 
{ 
    if (listBox1.SelectedItem == null || (int)numericUpDown1.Value == 0 || 
     (listBox1.SelectedItem as Product).Quantity < (int)numericUpDown1.Value) return; 

    foreach (Product item in listBox2.Items) 
    { 
     if (item.ID == (listBox1.SelectedItem as Product).ID) 
     { 
      return; 
     } 
    } 

    listBox2.Items.Add(new Product { Name = (listBox1.SelectedItem as Product).Name, 
           Price = (listBox1.SelectedItem as Product).Price, 
           Quantity = (int)numericUpDown1.Value}); 

    (listBox1.SelectedItem as Product).Quantity -= (int)numericUpDown1.Value; 
} 

這很好,除了它將一個新項目添加到我的產品表。我想要做的事情是向顧客購物車添加選定數量的選定產品,而不是將新項目添加到產品表格中,並使用numericupdown1.value減少訂購產品數量。我使用EF代碼第一個數據庫,看起來像:

public class Order 
{ 
    public int ID { get; set; } 
    public bool Status { get; set; } 

    public virtual Account account { get; set; } 
    public virtual List<Product> products { get; set; }   
} 

public class Product 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public long Price { get; set; } 
    public int Quantity { get; set; } 

    public virtual List<Order> orders { get; set; }  
} 

這裏是保存方法:

private void button1_Click(object sender, EventArgs e) 
{ 
    List<Product> p = new List<Product>(); 
    foreach (Product item in listBox2.Items) 
    { 
     p.Add(item); 
    } 
    Variables.Db.orders.Add(new Order { account = Variables.Currentuser, products = p }); 
    Variables.Db.SaveChanges(); //Variables.Db is my CodeFirst Database 
} 

我試過很多方法,例如:

int q = (listBox1.SelectedItem as Product).Quantity; 
(listBox1.SelectedItem as Product).Quantity = (int)numericUpDown1.Value; 
listBox2.Items.Add(listBox1.SelectedItem); 

(listBox1.SelectedItem as Product).Quantity = q - (int)numericUpDown1.Value; 

但最後一行將listbox2對象的數量改回爲與listbox1s相同。

回答

3

您正在使用相同的實體(Product)出於兩種不同的目的(產品和訂單項),這只是錯誤的。

你看到的第一個行爲是任何命令創建新Product對象的結果,而第二個是因爲你使用的是同一個產品對象兩個列表框。

正確的方式做,這是創建一個新的實體訂單項目:

public class Product 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public long Price { get; set; } 
    public int Quantity { get; set; } 

    public virtual List<OrderItem> ordersItems { get; set; } 
} 

public class Order 
{ 
    public int ID { get; set; } 
    public bool Status { get; set; } 

    public virtual Account account { get; set; } 
    public virtual List<OrderItem> Items { get; set; }   
} 

public class OrderItem 
{ 
    public int ID { get; set; } 
    public int Quantity { get; set; } 
    public virtual Product Product { get; set; } 
    public long TotalPrice { get; set; } // It is better to hold the price. What if you later changed the product price? 
} 

然後你就可以輕鬆地處理採購:

private void button2_Click(object sender, EventArgs e) //puts selectedproduct to the cart 
{ 
    Product p = listBox1.SelectedItem as Product; 
    int q = (int)numericUpDown1.Value; 
    if (p == null || q == 0 || p.Quantity < q) 
     return; 

    foreach (Product item in listBox2.Items) 
    { 
     if (item.ID == p.ID) 
     { 
      return; 
     } 
    } 

    listBox2.Items.Add(new OrderItem { Product = p, Quantity = q, TotalPrice = p.Price * q }); 
    p.Quantity -= q; 
} 


private void button1_Click(object sender, EventArgs e) 
{ 
    Order o = new Order() { account = Variables.Currentuser, Items = new List<Order>() }; 
    foreach (OrderItem item in listBox2.Items) 
    { 
     o.Items.Add(item); 
    } 
    Variables.Db.orders.Add(o); 
    Variables.Db.SaveChanges(); //Variables.Db is my CodeFirst Database 
} 

您是否也注意到了更清晰的代碼? :)

還有一件事:請始終使用有意義的名稱爲您的控件和類字段:btnSave,btnAddToCartnumQuantity是很好的候選人。

相關問題