2017-03-29 3 views
0

Windows Forms에서 C#을 사용하여 단추를 클릭 할 때 개체의 특성을 표시하려고하며이를 수행하는 방법을 모르겠습니다. 내 코드의 일부는 지금까지 이와 같습니다. 저는 C#의 초보자입니다.Windows 폼에서 단추를 클릭 할 때 개체의 특성을 표시하는 방법은 무엇입니까?

class Pizza: ICloneable 
    { 
     private string nume; 
     private int nrIngrediente; 
     private string[] ingrediente; 

     public Pizza() 
     { 
      nume = "Margherita"; 
      nrIngrediente = 2; 
      ingrediente = new string[nrIngrediente]; 
      for (int i = 0; i < nrIngrediente; i++) 
      { 
       ingrediente[0] = "Sos rosii"; 
       ingrediente[1] = "Mozzarella"; 
      } 
     } 



     public Pizza(string den, int nri, string[] ing) 
     { 
      nume = den; 
      nrIngrediente = nri; 
      ingrediente = new string[nrIngrediente]; 
      for (int i = 0; i < nrIngrediente; i++) 
       ingrediente[i] = ing[i]; 
     } 

     public Pizza(Pizza p) 
     { 
      nume = p.nume; 
      nrIngrediente = p.nrIngrediente; 
      ingrediente = new string[nrIngrediente]; 
      for (int i = 0; i < nrIngrediente; i++) 
       ingrediente[i] = p.ingrediente[i]; 
     } 

     public string PizzaName 
     { 
      get { return nume; } 
      set { nume = value; } 
     } 

public int PizzaNrIng 
     { 
      get { return nrIngrediente; } 
      set { nrIngrediente = value; } 


//also, i don't know how to write the getter and setter for this one 
     public string PizzaIngredients 
     //{ 
     // get 
     // { 
     //  for(int i=0;i<nrIngrediente;i++) 
     //   return ingrediente[i]; 
     // } 
     // set { ingrediente = value; } 
     //} 

그리고 지금은, 형태 코드는 (내가 이미 설계 참고) 다음과 같다 :

public partial class ListaPizza : Form 
    { 

     public ListaPizza() 
     { 
      InitializeComponent(); 
     } 

     private void Margherita_Click(object sender, EventArgs e) 
     { 

      string[] ingrMargh = new string[2] { "Sos rosii", "Mozzarella" }; 
      Pizza Margherita = new Pizza("Margherita", 2, ingrMargh); 

      //Show(Margherita); 
//here i want the object created above to be shown in a messagebox when i click the button in the form but i don't know how 


     } 
    } 

감사합니다!

+0

내가 당신이 원하는 정확히 이해 모르겠지만, 당신은 정확하게 표시하는'ToString' 메소드를 오버라이드 (override) 할 수 :

public string GetDisplayMessageForPizza() { return "My Pizza is " + nume + ". It contains " + nringrediente + " ingredients : " + PizzaIngredients; } 

및 양식 ListaPizza에 : 피자 클래스에 다음과 같이 구현 될 수있다 그리고 당신은'MessageBox.Show (Marguerita.ToString()); ' – Pikoh

답변

0

당신을 아래 getter와 setter를 설정할 수 있습니다.

그리고 쇼 (마게 리타);

private void Margherita_Click(object sender, EventArgs e) 
{ 

    string[] ingrMargh = new string[2] { "Sos rosii", "Mozzarella" }; 
    Pizza Margherita = new Pizza("Margherita", 2, ingrMargh); 

    MessageBox.Show(Margherita.GetDisplayMessageForPizza()); 
} 
+1

과 같은 것을 할 필요가 있습니다. 문자열은 불변이므로 문자열을 "+"로 연결하는 것은 나쁜 습관입니다. string의 새로운 인스턴스. string.Format 또는 StringBuilder를 사용하는 것이 더 좋습니다 –

+0

나는 나쁜 습관을 알고 있습니다. 방금 질문자가 예상 한 답변을 게시했습니다. 다음 번부터 조심하겠습니다. GetDisplayMessageForPizza()에서 코더는 표시 할 필수 형식의 문자열을 반환 할 수 있습니다. – Vandita

+0

그것은 잘 작동하지만 재료를 보여 주면 다음과 같이 말합니다 : 그것은 두 가지 성분을 포함합니다 : System.String []. 그것은 C++에서 더 쉽게 읽을 수 있습니다. 하하 @Vandita – Anais

1

MessageBox.Show();

은 문자열 값이어야하므로 문자열이 매소드로 .toString()가 필요없는 개체의 어떤 부분

또는 당신은 당신이 새로운 형태로 개체를 전달할 수있는보기를 사용자 정의하려면 디자이너와 함께 멋진 레이아웃을 만들 수 있습니다. 게터에 관해서는

0

- 당신이 문자열 배열을 반환해야합니다,뿐만 아니라 문자열 : 상영에 관해서는

public string[] PizzaIngredients 
{ 
    get 
    { 
     ingrediente; 
    } 
    set 
    { 
     ingrediente = value; 
    } 
} 

- 단지 MessageBox.Show()를 사용 :

MessageBox.Show(string.Format("{0} {1}", Margherita.Name, Margherita.nrIngrediente));