C# 如何使用另一个类中的方法添加到列表框?
我觉得我错过了一些明显的东西 我有两个类,ShoppingBasket和OrderItem,然后是Form1类。我想在ShoppingBasket中使用OrderItem中的四个属性。我想在textbox1中获取产品名称,在numericupdown1中获取数量,在textbox2中获取最新价格,然后我将单击add按钮,该按钮将使用OrderItem类验证这些值,然后将它们放入ShoppingBasket类中的AddProduct方法中,该方法有望在表单的列表框中添加一行 表格1:C# 如何使用另一个类中的方法添加到列表框?,c#,winforms,class,listbox,shopping-cart,C#,Winforms,Class,Listbox,Shopping Cart,我觉得我错过了一些明显的东西 我有两个类,ShoppingBasket和OrderItem,然后是Form1类。我想在ShoppingBasket中使用OrderItem中的四个属性。我想在textbox1中获取产品名称,在numericupdown1中获取数量,在textbox2中获取最新价格,然后我将单击add按钮,该按钮将使用OrderItem类验证这些值,然后将它们放入ShoppingBasket类中的AddProduct方法中,该方法有望在表单的列表框中添加一行 表格1: public
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; }
}
您的问题是,无论何时添加产品,您都是在从您的
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);
}
另外一个继续的一般建议是更改您的设计。目前,ShoppingBasket
与Form1
高度耦合-这意味着您不能从Form1
以外的任何来源向购物篮添加新项目!但是ShoppingBasket
不应该关心它收到的物品的来源。此外,在您每次插入商品时,都会创建一个新的购物篮。这意味着,每个购物篮只能有一件物品。因此,为了进一步学习,我建议以下几点:
- 使
ShoppingBasket
成为Form1
的成员变量
- 添加项时,将该项添加到此成员变量
- 不要将您的表单传递给
AddProduct
,而是让您的ShoppingBasket
提供它包含的项目的信息
- 调用
listBox1.Items。在AddProduct
之后添加
然后,您的购物篮
不关心您的产品如何呈现,它只关心产品如何在内部存储。您遇到了什么问题?为什么不返回字符串并在表单中添加,而不是在方法中添加?您的方法不必是void
@PoweredByOrange-它说“listBox1由于其保护级别而无法访问”,所以我认为我这样做不对。有什么想法吗?@YoryeNathan我这样做是因为它是根据一本小册子编写的,但我们只得到了类名和方法名。@user2299950这是因为列表框属于表单,而不是购物篮
类。您需要将订单项发送到form1,并让它将其添加到列表中。