首页 > 解决方案 > 将对象列表传递给主窗体

问题描述

我有运行应用程序时打开的主窗体 Form1。我将此表单中包含供应商对象的列表传递给辅助 Form2,以帮助我使用存储在 Form1 的供应商列表中的对象属性构建产品对象。

在 Form2 中,我有一个 Product 对象列表,在我完成它并在 ListView 中显示后,我想将其传回给 Form1。但是有些东西不起作用..我不知道是什么。先感谢您。

表格1:

public partial class Form1 : Form
    {
        public ArrayList suplist = new ArrayList(); //suppliers list
        public List<Product> productlist = new List<Product>(); //products list which will be populated with objects sent from Form2


    public Form1()
    {
        InitializeComponent();

        //Read Suppliers from txt
        StreamReader sr = new StreamReader("Suppliers.txt");
        string linie = null;
        while((linie=sr.ReadLine())!=null) {
            try
            {
                int id = Convert.ToInt32(linie.Trim().Split(',')[0]);
                string nume = linie.Trim().Split(',')[1];

                Supplier sp = new Supplier(id, nume);
                suplist.Add(sp);
            }
            catch(Exception ex) { MessageBox.Show(ex.Message); }
        }

        listView1.Columns.Add("ID");
        listView1.Columns.Add("Nume");
        listView1.Columns.Add("Units");
        listView1.Columns.Add("Price");
        listView1.Columns.Add("SUpplier Id");
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 from = new Form2(suplist);
        from.ShowDialog();
    }

    public List<Product> ProductList
    {
        get { return productlist; }
        set { productlist = value; }
    }



      private void button2_Click(object sender, EventArgs e)
        { //this function is supposed to populate listview with the productlist objects when i click the button;
//not sure if it is wrong writeed, or passing the list of products created in Form2 failed
            foreach (Product p in productlist)
            {
                //listView1.Items.Add(p.Id);
                ListViewItem itm = new ListViewItem(p.Id.ToString());
                itm.SubItems.Add(p.Nume);
                itm.SubItems.Add(p.Units.ToString());
                itm.SubItems.Add(p.Price.ToString());
                itm.SubItems.Add(p.SupplierId.ToString());

            }
        }
    }

表格2:

    public partial class Form2 : Form
        {
            public List<Product> prodList = new List<Product>(); //list which stores the Products == > the list i want to send back to Form 1 
        public ArrayList supplierList = new ArrayList(); //list of suppliers received from From 1, used to build Products objects


    public Form2(ArrayList suplist)
    {
        InitializeComponent();
        supplierList = suplist;

        foreach(Supplier s in supplierList)
        {
            comboBox1_supID.Items.Add(s.Id);
        }

        Product p1 = new Product(1, "Cola", 4, 45, 1);
        Product p2 = new Product(2, "Fanta", 32, 22, 2);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (textBox1__id.Text == "") errorProvider1.SetError(textBox1__id, "Introduceti id");
        else if (textBox2_nume.Text == "") errorProvider1.SetError(textBox2_nume, "Introduceti numele");
        else if (textBox3_units.Text == "") errorProvider1.SetError(textBox3_units, "Introduceti units");
        else if (textBox4_price.Text == "") errorProvider1.SetError(textBox4_price, "enter price");
        else if (comboBox1_supID.Text == "") errorProvider1.SetError(comboBox1_supID, "Select sup id");
        else 
            try
            {
                int id = Convert.ToInt32(textBox1__id.Text);
                string nume = textBox2_nume.Text;
                int units = Convert.ToInt32(textBox3_units.Text);
                double price = Convert.ToDouble(textBox4_price.Text);
                int supid = Convert.ToInt32(comboBox1_supID.Text);

                Product pd = new Product(id, nume, units, price, supid);
                prodList.Add(pd);
                MessageBox.Show("Produs adaugat cu succes");

            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                textBox1__id.Clear();
                textBox2_nume.Clear();
                textBox4_price.Clear();
                textBox3_units.Clear();
                errorProvider1.Clear();
            }
    }

    private void textBox4_price_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!char.IsDigit(e.KeyChar))
        {
            errorProvider1.SetError(textBox4_price, "Introduceti numai cifre");
        }
        else errorProvider1.Clear();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Form1 frm = new Form1();
        frm.productlist = prodList;
        frm.Show();
    }
}

我想从Form2发送到Form1 prodList,(将它存储在我猜Form1中的productlist中)并将它们显示在Form1的listview1中。

简而言之,在 Form1 中,我创建了供应商,将它们存储在 suplist 中并将此列表传递给 Form2(在供应商列表中)。在 Form2 中,我创建产品,将它们存储在 prodList 中并将其传递给 Form1(在 productList 中)。为什么不工作?为什么listview什么都不显示??

标签: winformslistviewarraylist

解决方案


我还没有使用 ListView-Object 本身,但我认为您错过了将创建的 ListViewItem 添加到您的 ListView

foreach (Product p in productList)
{
   ListViewItem itm = new ListViewItem(p.Id.ToString());
   itm.SubItems.Add(p.Nume);
   itm.SubItems.Add(p.Units.ToString());
   itm.SubItems.Add(p.Price.ToString());
   itm.SubItems.Add(p.SupplierId.ToString());

   listView1.Items.Add(itm);
}

推荐阅读