简体   繁体   中英

How can I put variables in Form1 class listBox from another class?

How can I put variables into Form1 class listBox from another class?

I declare variables in Form2 window to third class named Product by properties. After this the second window should close and send those variables to MainWindow (Form1) listBox. I tryed to make instruction putting variables from Form2 window to MainWindow , but I am getting a NullException error. Now code looks like this:

Class Form1 :

public partial class Form1 : Form
{
    Form2 fileAdd;
    Product product;

    public Form1()
    {
        InitializeComponent();
        fileAdd = new Form2();
        product = new Product();
    }

    public void actualizationButton_Click(object sender, EventArgs e)
    {
        if (product.Name == statusList.Text) // if product is in the listBox
        {
            // the instruction that will be performed if the given product is on the listBox
            for (int i = product.Quantity; i < int.Parse(quantityTextBox.Text); i++)
            {
                product.Quantity++;
            }
        }
        else
        {
            fileAdd.Show(); // launches the window to adding a new item

            if (product.Name.Length != 0 && product.Code.Length != 0 &&
                product.Reference.Length != 0 && product.Manufacturer.Length != 0)
            {
                statusList.Items.Add(product.Name + "\t" + product.Reference + "\t" +
                product.Manufacturer + "\t" + product.Quantity);

                product.Name = "";
                product.Code = "";
                product.Reference = "";
                product.Manufacturer = "";
                product.Quantity = 0;
                // how to execute the instruction to transfer variables 
                //to listBox Form1 after closing Form2 window?
            }
            else
            {
                MessageBox.Show("Not included item!");
            }
        }
    }
}

Class Form2 :

public partial class Form2 : Form
{
    Product product;

    public Form2()
    {
        InitializeComponent();
        product = new Product();
    }

    private void fileProductAddButton_Click(object sender, EventArgs e)
    {
        if (product.Name.Length != 0 && product.Reference.Length != 0)
        {
            product.Name = fileNameTextBox.Text;
            product.Code = fileCodeTextBox.Text;
            product.Reference = fileReferenceTexBox.Text;
            product.Manufacturer = fileManufacturerTextBox.Text;

            MessageBox.Show("Added " + product.Name + " with reference " + product.Reference +
                "\nManufacturer " + product.Manufacturer + "\nQuantity " + product.Quantity);

            this.Close();
        }
        else
        {
            MessageBox.Show("Fill in all fields!");
        }
    }
}

Class Product :

class Product
{
    public string Name { get; set; } = "N/A";
    public string Code { get; set; } = "N/A";
    public string Reference { get; set; } = "N/A";
    public string Manufacturer { get; set; } = "N/A";
    public int Quantity { get; set; } = 0;
}

Pushing data from one form to another should be a basic thing, there are several methods to do that. First: implement a proper constructor which accepts a Product object. Second option is to create a public Property which can be filled with the data after creation of the form. And third, you can have a static field, holind gyour data. Every option is up to you and your design.

TinoZ suggestions are fine, but seeing you are a beginner, I'll add a little bit of code to illustrate the concepts.
Clearly your problem is that Form1 and Form2 have completely separate instances of a product. Whatever you do with the product in Form2 has no effect in Form1.

Your main form creates an instance of Form2 - here you can take the new instance of the Product and pass it to 'Form2' as a dependency, like below:

        Form2 fileAdd;
        Product product;

        public Form1()
        {
            InitializeComponent();
            product = new Product();
            fileAdd = new Form2(product);
            
        }

Then, your Form2 constructor needs to understand this parameter and assign it to it's field:

        Product product;

        public Form2(Product product) //this is the product from main form
        {
            InitializeComponent();
            this.product = product; //here you are making Form2 work on the same product that Form1 has.
        }

Lastly, please tidy up your code, remove the 'noise' and if you are getting any errors (like null reference), post them as well.

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