2

我觉得我在这里遗漏了一些明显的东西。这是我的表格的截图。

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

表格1:

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

购物篮:

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

订单项:

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

1 回答 1

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()没有被调用),列表项也会被添加到这个“不可见”的表单中,而不是原来的表单中。

为了解决这个问题,我建议将您的表单作为参数传递给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);
}

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

  • ShoppingBasket一个成员变量Form1
  • 添加项目时,将项目添加到此成员变量中。
  • 不要将您的表单传递给AddProduct,而是让您ShoppingBasket提供有关它包含的项目的信息。
  • listBox1.Items.Add之后马上打电话AddProduct

那么您ShoppingBasket并不关心您的产品如何呈现,它只关心产品如何在内部存储。

于 2013-07-19T07:53:05.713 回答