나는 여기에서 명백한 것을 놓치고있는 것처럼 느낀다. This is a screenshot of my form.다른 클래스의 메서드를 사용하여 ListBox에 어떻게 추가합니까?
두 개의 클래스 ShoppingBasket 및 OrderItem이 있고 Form1 클래스가 있습니다. ShoppingBasket에서 사용하려는 OrderItem에는 4 개의 특성이 있습니다. textbox1의 제품 이름, numericupdown1의 수량 및 textbox2의 최신 가격을 가져 오려면 OrderBox 클래스를 사용하여 값의 유효성을 검사 할 Add 단추를 클릭 한 다음 ShoppingBasket 클래스의 AddProduct 메서드에 넣으십시오. ListBox에 양식을 추가 할 수 있습니다.
를 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; }
}
무엇이 문제입니까? – PoweredByOrange
그냥 문자열을 반환하고 양식에 추가하는 대신 왜 방법에 추가? 당신의 방법은 '무효'일 필요는 없습니다. – SimpleVar
@PoweredByOrange - 'listBox1은 해당 보호 수준으로 인해 액세스 할 수 없습니다.'라는 메시지가 표시되므로 제대로 처리하지 못했다고 생각됩니다. 어떤 생각? –