ここで明らかな何かが欠けているように感じます。これは私のフォームのスクリーンショットです。
ShoppingBasket と OrderItem の 2 つのクラスと、Form1 クラスがあります。OrderItem には、ShoppingBasket で使用したい 4 つのプロパティがあります。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; }
}