簡體   English   中英

單擊Windows窗體中的按鈕時如何顯示對象的屬性?

[英]How to show the attributes of an object when clicking a button in Windows forms?

我想在使用c#單擊Windows窗體中的按鈕時顯示對象的屬性,但我不知道該怎么做。 到目前為止,我的部分代碼看起來像這樣。 我是C#的初學者。

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


        } 
    }

謝謝!

MessageBox.Show();

必須為字符串值,因此對象的任何非字符串部分都需要.ToString()方法

或者,如果您想自定義視圖,則可以將對象傳遞給新表單,並與設計者一起為其創建一個完整的布局。

至於getter-您應該返回一個字符串數組,而不僅僅是一個字符串:

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

至於顯示-只需使用MessageBox.Show():

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

您可以通過以下方式設置getter和setter。

public string PizzaIngredients
{
    get
    {
        return String.Join(",",nringrediente);
    }
    set
    {
        nringrediente = value.Split(',');
    }
}

和秀(瑪格麗特); 可以像比薩餅類中那樣實現:

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

並以ListaPizza的形式:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM