繁体   English   中英

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

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

如何将变量从另一个 class 放入Form1 class 列表框?

我将Form2 window 中的变量声明为按属性命名的第三个Product变量。 在此之后,第二个 window 应该关闭并将这些变量发送到MainWindow (Form1) 列表框。 我尝试将变量从 Form2 window 放入MainWindow的指令,但我收到NullException错误。 现在代码如下所示:

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!");
            }
        }
    }
}

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
{
    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;
}

将数据从一种形式推送到另一种形式应该是一件基本的事情,有几种方法可以做到这一点。 首先:实现一个接受Product object 的适当构造函数。 第二种选择是创建一个公共属性,可以在创建表单后填充数据。 第三,您可以拥有一个 static 字段,保存您的数据。 每个选项都取决于您和您的设计。

TinoZ 的建议很好,但鉴于您是初学者,我将添加一些代码来说明这些概念。
显然,您的问题是 Form1 和 Form2 具有完全独立的产品实例。 无论您对 Form2 中的产品做什么,对 Form1 都没有影响。

您的主表单创建了 Form2 的实例 - 在这里您可以获取 Product 的新实例并将其作为依赖项传递给“Form2”,如下所示:

        Form2 fileAdd;
        Product product;

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

然后,您的 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.
        }

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

暂无
暂无

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

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