我觉得我在这里遗漏了一些明显的东西。这是我的表格的截图。
我有两个类,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; }
}