在.showDialog之后刷新并在列表中显示项目 - c#.NET

时间:2018-05-08 19:49:07

标签: c# .net

目前,在我的程序中,用户打开表单1以创建类的新实例,然后将其保存到列表中。在表单1关闭后,我希望主表单重新加载并在屏幕上显示更新的列表。我无法弄清楚如何刷新主导航以及如何将列表显示在表单上。

MainNaviagation

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

            m_modelObj = modelObj;

            m_modelObj.ChocolateAdded += m_modelObj_ChocolateAdded;
        }

        void m_modelObj_ChocolateAdded(Chocolate newChocolate)
        {

            //whole list of chocolates
            List<Chocolate> chocolateList = m_modelObj.ChocolateList;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string candy = comboBox1.SelectedItem.ToString();
            Form1 aForm1 = new Form1(textBox1.Text, candy, m_modelObj);
            aForm1.ShowDialog();

        }


    }

模特课程:

{
    public delegate void ChocolateAddedEventHander(Chocolate newChocolate);
    public class Model
    {
        public event ChocolateAddedEventHander ChocolateAdded;


        public List<Chocolate> ChocolateList = new List<Chocolate>();
        public void AddChocolateInList(Chocolate chocolate)
        {
            ChocolateList.Add(chocolate);

            if (ChocolateAdded != null)
                ChocolateAdded(chocolate);
        }
    }

form1

public partial class Form1 : Form
    {
        Model m_model;
        public Form1(string name, string candy, Model modelObj)
        {

            InitializeComponent();
            m_model = modelObj;

            string str = name + " selected : ";
            label1.Text = str;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Chocolate newChocolate = new Chocolate(comboBoxChocolateSelection.SelectedItem.ToString(), 12.5, true, 2);
            m_model.AddChocolateInList(newChocolate);

            this.Close();

        }

    }

巧克力

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)
        {
            this.flavor = flavor;
            this.cost = cost;
            this.giftWrap = giftWrap;
            this.quantity = quantity;
        }
        #endregion End of Constructors


    }

0 个答案:

没有答案