2013-07-18 33 views
2

我觉得我在这里错过了一些明显的东西。 This is a screenshot of my form.如何使用另一个类中的方法添加到ListBox?

我有两个类ShoppingBasket和OrderItem,然后Form1类。 OrderItem中有四个属性,我想在ShoppingBasket中使用。我想在textbox1中获取产品名称,numericupdown1中的数量和textbox2中的最新价格,然后单击添加按钮,该按钮将使用OrderItem类验证值,然后将它们放入ShoppingBasket类的AddProduct方法中这将有望在表单中向列表框中添加一行。

Form1中:

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void addButton_Click(object sender, EventArgs e) 
    { 
     decimal latestPrice; 

     ShoppingBasket addButtonShoppingBasket = new ShoppingBasket(); 

     decimal.TryParse(textBox2.Text, out latestPrice); 
     OrderItem currentItemQuantity1 = new OrderItem(textBox1.Text, latestPrice, Convert.ToInt32(numericUpDown1.Value)); 

     addButtonShoppingBasket.AddProduct(currentItemQuantity1.ProductName, currentItemQuantity1.LatestPrice, currentItemQuantity1.Quantity); 
    } 
} 

ShoppingBasket:

public class ShoppingBasket 
{ 
    public ShoppingBasket() 
    { 

    } 

    public void AddProduct(string productName, decimal latestProductValue, int quantity) 
    { 
     Form1 newform = new Form1(); 

     string itemFormatString = "{0,-50}{1,0}{2,50}"; 
     newform.listBox1.Items.Add(string.Format(itemFormatString, productName, Convert.ToString(quantity), Convert.ToString(latestProductValue))); 
    } 
} 

OrderItem的:

public class OrderItem 
{ 
    public OrderItem(string productName, decimal latestPrice, int quantity) 
    { 
     ProductName = productName; 
     LatestPrice = latestPrice; 
     Quantity = quantity; 
     TotalOrder = latestPrice * quantity; 
    } 

    public string ProductName { get; set; } 

    public decimal LatestPrice { get; set; } 

    public int Quantity { get; set; } 

    public decimal TotalOrder { get; set; } 
} 
+1

你有什么问题? – PoweredByOrange

+2

为什么不直接返回字符串并添加到表单中,而不是在方法中添加?你的方法不一定是'void'。 – SimpleVar

+0

@PoweredByOrange - 它说'listBox1由于其保护级别而无法访问',所以我认为我没有这样做。任何想法? –

回答

1

你的问题是,你正在创建从ShoppingBasked一种新的形式,只要产品是补充:

public void AddProduct(string productName, decimal latestProductValue, int quantity) 
{ 
    Form1 newform = new Form1(); 

    string itemFormatString = "{0,-50}{1,0}{2,50}"; 
    newform.listBox1.Items.Add(string.Format(itemFormatString, productName, Convert.ToString(quantity), Convert.ToString(latestProductValue))); 
} 

newform不是实际叫做AddProduct的形式!即使你没有在任何地方看到这个newform(因为newform.Show()没有被调用),列表项get被添加到这个“不可见”形式,而不是原来的形式。

为了解决这个问题,我建议你通过你的形式参数AddProduct

public void AddProduct(Form1 form, string productName, decimal latestProductValue, int quantity) 
{ 
    string itemFormatString = "{0,-50}{1,0}{2,50}"; 
    form.listBox1.Items.Add(string.Format(itemFormatString, productName, Convert.ToString(quantity), Convert.ToString(latestProductValue))); 
} 

,并调用它是这样的:

private void addButton_Click(object sender, EventArgs e) 
{ 
    // ... 
    // Your current code here 
    // ... 

    addButtonShoppingBasket.AddProduct(this, 
     currentItemQuantity1.ProductName, 
     currentItemQuantity1.LatestPrice, 
     currentItemQuantity1.Quantity); 
} 

也是一个一般性的建议,继续在改变你的设计。目前,ShoppingBasketForm1高度耦合 - 这意味着,您不能从Form1以外的任何其他来源向您的购物篮中添加新项目!但ShoppingBasket不应该关心它收到的项目的来源。此刻,您每次插入项目时都会创建一个新的ShoppingBasket。这意味着,每ShoppingBasket只能有一个项目。因此,为了进一步学习,我建议遵循以下几点:

  • 使ShoppingBasket成员变量Form1
  • 添加项目时,将项目添加到此成员变量。
  • 请勿将您的表单传递给AddProduct,而应让您的ShoppingBasket提供有关其包含的项目的信息。
  • 致电listBox1.Items.AddAddProduct之后。

然后你的ShoppingBasket不关心你的产品是如何呈现的,它只关心产品如何存储在内部。

相关问题