将对象列表传递到主表单

时间:2018-06-20 01:03:01

标签: winforms listview arraylist

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

在Form2中,我具有要完成后的产品对象列表,并在ListView中显示该产品对象。但是有些东西不起作用..我不知道是什么。预先谢谢你。

Form1:

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());

            }
        }
    }

Form2:

    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中,我创建了Suppliers,将其存储在suplist中,并将此列表传递给Form2(在Supplierlist中)。在Form2中,我创建了Products,将它们存储在prodList中并将其传递给Form1(在productList中)。为什么不工作?以及为什么listview不显示任何内容?

1 个答案:

答案 0 :(得分:0)

我还没有使用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);
}