简体   繁体   中英

C# : Modifying form1's listbox using form2's command button

I'm trying to make this simple program using Visual Studio C# 2013. program screenshot: http://i.imgur.com/4QVbaa2.png

The listbox named receiptbox modifier was set to public using the properties panel.

Basically I am using 2 forms, what I want to happen is to show the quantity + the name of the food in the form 1's listbox.

This is the code when you click the food icon on form1:

 private void pictureBox1_Click(object sender, EventArgs e)
        {
            FoodQty form2 = new FoodQty();
            form2.Show();

        }

It will show form2.

This is the source-code in the form2 and when you click its Ok button:

public partial class FoodQty : Form
    {
        Form1 mainfrm = new Form1();
        Record recordInstance = new Record();
        public FoodQty()
        {
            InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            mainfrm.receiptBox.Items.Add((int)numericUpDown1.Value + recordInstance.foodMenuArray[1]); // converts numupdown to int and appends the string array


        }
    }

Try this : in form 1 :

 private void pictureBox1_Click(object sender, EventArgs e)
        {
            FoodQty form2 = new FoodQty(this);
            form2.Show();

        }

in fom2 :

public partial class FoodQty : Form
    {
        Form1 mainfrm = new Form1();
        Record recordInstance = new Record();
        public FoodQty( Form1 fr)
        {
            InitializeComponent();
             mainfrm =fr;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            mainfrm.receiptBox.Items.Add((int)numericUpDown1.Value + recordInstance.foodMenuArray[1]); // converts numupdown to int and appends the string array


        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM