簡體   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