2013-07-18 2 views
2

나는 여기에서 명백한 것을 놓치고있는 것처럼 느낀다. 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; } 
} 
+1

무엇이 문제입니까? – PoweredByOrange

+2

그냥 문자열을 반환하고 양식에 추가하는 대신 왜 방법에 추가? 당신의 방법은 '무효'일 필요는 없습니다. – SimpleVar

+0

@PoweredByOrange - 'listBox1은 해당 보호 수준으로 인해 액세스 할 수 없습니다.'라는 메시지가 표시되므로 제대로 처리하지 못했다고 생각됩니다. 어떤 생각? –

답변

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()이 호출되지 않기 때문에) 목록 항목 get이 원래 "notvisible"양식에 추가됩니다.

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

을 그리고 다음과 같이 호출 :

내가 AddProduct에 매개 변수로 양식을 전달하는 것이 좋습니다,이 문제를 해결하려면 또한

private void addButton_Click(object sender, EventArgs e) 
{ 
    // ... 
    // Your current code here 
    // ... 

    addButtonShoppingBasket.AddProduct(this, 
     currentItemQuantity1.ProductName, 
     currentItemQuantity1.LatestPrice, 
     currentItemQuantity1.Quantity); 
} 

일반적인 조언 설계를 변화하고 계속합니다. 현재 ShoppingBasketForm1과 잘 결합되어 있습니다. 즉, Form1 이외의 다른 출처에서 쇼핑 바구니에 새 항목을 추가 할 수 없습니다. 그러나 ShoppingBasket은 수신하는 항목의 출처를 신경 쓰지 않아야합니다. 또한 순간에 항목을 삽입 할 때마다 ShoppingBasket을 새로 만듭니다. 즉, ShoppingBasket 당 하나의 항목 만 가질 수 있습니다. 따라서 추가 학습을 위해 다음 요점을 따르는 것이 좋습니다.

  • 의 구성원 변수를 Form1으로 설정하십시오.
  • 항목을 추가 할 때이 멤버 변수에 항목을 추가하십시오.
  • 양식을 AddProduct으로 전달하지 말고 ShoppingBasket에 포함 된 항목에 대한 정보를 제공하십시오.
  • AddProduct 바로 뒤에 listBox1.Items.Add으로 전화하십시오.

ShoppingBasket은 제품이 어떻게 표시되는지에 상관없이 제품이 내부적으로 저장되는 방법에만 관심이 있습니다.

관련 문제