简体   繁体   English

如何从另一个 class 将变量放入 Form1 class listBox 中?

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

How can I put variables into Form1 class listBox from another class?如何将变量从另一个 class 放入Form1 class 列表框?

I declare variables in Form2 window to third class named Product by properties.我将Form2 window 中的变量声明为按属性命名的第三个Product变量。 After this the second window should close and send those variables to MainWindow (Form1) listBox.在此之后,第二个 window 应该关闭并将这些变量发送到MainWindow (Form1) 列表框。 I tryed to make instruction putting variables from Form2 window to MainWindow , but I am getting a NullException error.我尝试将变量从 Form2 window 放入MainWindow的指令,但我收到NullException错误。 Now code looks like this:现在代码如下所示:

Class Form1 : Form1表格1:

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 : Form2表格2:

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

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.首先:实现一个接受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.第三,您可以拥有一个 static 字段,保存您的数据。 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. TinoZ 的建议很好,但鉴于您是初学者,我将添加一些代码来说明这些概念。
Clearly your problem is that Form1 and Form2 have completely separate instances of a product.显然,您的问题是 Form1 和 Form2 具有完全独立的产品实例。 Whatever you do with the product in Form2 has no effect in Form1.无论您对 Form2 中的产品做什么,对 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 的实例 - 在这里您可以获取 Product 的新实例并将其作为依赖项传递给“Form2”,如下所示:

        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:然后,您的 Form2 构造函数需要了解此参数并将其分配给它的字段:

        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.最后,请整理您的代码,删除“噪音”,如果您遇到任何错误(如 null 参考),请同时发布它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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