Trouble passing new object back to main form - C#

2019-08-23 07:03发布

I am new to C# and the .NET framework. I am trying to create an array of chocolate orders that shows up as a list on the main screen(MainNavigation). I have a chocolate class and a form(form1) where the user can select which type of chocolate and a new chocolate object is created. I am stuck on trying to pass the new object back to the main form and then showing it in a list on the main form.

MainNavigation form.... I would like value to be the orders that the user creates in form1.

using System;
using System.Windows.Forms;

namespace GatesCandyStore
{
    public partial class MainNavigation : Form
    {

        public MainNavigation()
        {
            InitializeComponent();
            Chocolate[] chocolates = new Chocolate[100];
            for (int runs = 0; runs < 100; runs++)
            {
                chocolates[runs] = value;
            }
        }

        private void btnProcessCandySelection_Click(object sender, EventArgs e)
        {
            string candy = comboBoxCandySelection.SelectedItem.ToString();
            Form1 aForm1 = new Form1(textBoxName.Text, candy);
            aForm1.ShowDialog();
        }
    }
}

Form1 where the user creates a new chocolate order.... Close(newChocolate); does not work.

using System;
using System.Windows.Forms;

namespace GatesCandyStore {
    public partial class Form1 : Form
    {
        Chocolate newChocolate = new Chocolate();
        public Form1(string name, string candy)
        {

            InitializeComponent();
            string str = name + " selected : ";
            label1.Text = str;
            Console.WriteLine(name + " selected : " + candy);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            newChocolate.Flavor = comboBoxChocolateSelection.SelectedItem.ToString();
            newChocolate.Cost = 12.5;
            newChocolate.GiftWrap = true;
            newChocolate.Quantity = 2;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            Close(newChocolate);
        }
    } }

Chocolate Class

namespace GatesCandyStore
{
    public class Chocolate
    {
        #region Fields
        public string flavor;
        public double cost;
        public bool giftWrap;
        public int quantity;

        #endregion End of Fields

        #region Constructors 
         public Chocolate(string flavor, double cost, bool giftWrap, int quantity)
         {
             Flavor = flavor;
             Cost = cost;
             GiftWrap = giftWrap;
             Quantity = quantity;
         }
         #endregion End of Constructors

         #region Properties
         public string Flavor { get; set; }
         public double Cost { get; set; }
         public bool GiftWrap { get; set; }
         public int Quantity { get; set; }
         #endregion End Properties


    }
}

1条回答
倾城 Initia
2楼-- · 2019-08-23 07:38

here In your question, I'm still not clear what you are doing candy and name in your Form1's constructor.

Assuming you know what you are doing with them and taking your main concern "How to pass newly created object back to main form", I'm suggesting you some correction.

  • Create A Model class

        public delegate void ChocolateAddedEventHander(Chocolate newChocolate);
        public class Model
        {
            //An Event which will be raised when you add new chocolate
            public event ChocolateAddedEventHander ChocolateAdded;
    
            //If at any point of program you need whole list of added chocolates
            public List<Chocolate> ChocolateList = new List<Chocolate>();
            public void AddChocolateInList (Chocolate chocolate)
            {
                ChocolateList.Add(chocolate);
    
                if (ChocolateAdded != null)
                    ChocolateAdded(chocolate);
            }
        }
    

Your Chocolate class will same as you have shown here.

  • Make following changes in Program.cs

    static void Main()
    {
        Model modelObj = new Model();
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MainNavigation(modelObj));
    }
    

As you can see in above code, MainNavigation is accecpting an object of Model class now.

  • MainNavigation form.

In code you shown in question, you seem to be doing something in constructor of MainNavigation , if array of Chocolate is containing all added chocolates you can use m_modelObj.ChocolateList there. But as I'm not clear what is value there. I'm not adding that part in my answer.

        public partial class MainNavigation : Form
        {
            private Model m_modelObj;
            public MainNavigation(Model modelObj)
            {
                InitializeComponent();
                m_modelObj = modelObj;

                //subscribing an even of Model class, 
                //this will handle your logic what you want to perform on adding new Chocolate
                m_modelObj.ChocolateAdded += m_modelObj_ChocolateAdded;
            }

            void m_modelObj_ChocolateAdded(Chocolate newChocolate)
            {
                //perform your task what you want to do with newly added chocolate


                //if you want whole list of chocolates
                List<Chocolate> chocolateList = m_modelObj.ChocolateList;
            }

            private void btnProcessCandySelection_Click(object sender, EventArgs e)
            {
                string candy = comboBoxCandySelection.SelectedItem.ToString();
                Form1 aForm1 = new Form1(textBoxName.Text, candy, m_modelObj);
                aForm1.ShowDialog();
            }
        }

Note that, now Form1 will accept three parameters, third as in object of Model class

  • Form1 where you are adding chocolate.

    public partial class Form1 : Form
    {
        Model m_model;
        public Form1(string name, string candy, Model modelObj)
        {
            InitializeComponent();
    
            m_model = modelObj;
    
            //Not sure what you are doing here, but it will work
            string str = name + " selected : ";
            label1.Text = str;
            Console.WriteLine(name + " selected : " + candy);
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            //adding new chocolate to list;
            Chocolate newChocolate = new Chocolate(comboBoxChocolateSelection.SelectedItem.ToString(), 12.5, true, 2);
            m_model.AddChocolateInList(newChocolate);
            this.Close();
        }
    }
    

One more thing I will suggest, naming of class and member should be proper, It will help a lot while debugging and code reviewing.

查看更多
登录 后发表回答