使用另一种形式的文本框更新listviewitems C#

时间:2018-06-21 00:03:37

标签: c# winforms listview textbox

我有Form1(主窗体),在其中我在ListView中加载在Form2中创建的产品列表。我创建了一个上下文菜单条,当我从listView中选择一行并在该上下文菜单中选择“编辑”选项时,将显示Form2,并且我用于创建产品的所有文本框都使用该行中包含的值来完成在ListView中选择。

我想要做的是能够编辑使用Form2中的textBoxes选择的行的一个或多个值,然后将更新后的产品列表发送回Form1以进行显示,而无需更改行的顺序就像如果我先删除该行,然后再次添加该行,但是更新了它。

谢谢。到目前为止,这是我在Form2中实现的:

 private void button3_Click(object sender, EventArgs e)
        {
            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
                {
                   // Product pSelected;
                    foreach (Product p in prodList)
                    {
                        if (p.Id == Convert.ToInt32(textBox1__id.Text))
                        {
                           // p.Id = Convert.ToInt32(textBox1__id.Text);
                            p.Nume = textBox2_nume.Text;
                            p.Units = Convert.ToInt32(textBox3_units.Text);
                            p.Price = Convert.ToDouble(textBox4_price.Text);
                            p.SupplierId = Convert.ToInt32(comboBox1_supID.Text);
                        }

                    }      

                    MessageBox.Show("Produs modificat cu succes"); 
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    textBox1__id.Clear();
                    textBox2_nume.Clear();
                    textBox4_price.Clear();
                    textBox3_units.Clear();
                    errorProvider1.Clear();
                    comboBox1_supID.ResetText();
                }


        }

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

            frm.Show();
        }

这是我使用在Form2中创建的产品列表填充到Form1中的listView,然后将其发送到Form1的方式:

private void button2_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();

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

            }
        }

1 个答案:

答案 0 :(得分:0)

我假设您在Form2中根据列表视图中的选定项目设置textBox1__id的文本。并且它也是不可编辑的(如果是,则必须将其设置为不可编辑)。

在这两种形式下都可以访问

产品列表,请确保您在两种形式之间都具有通用(共享)的产品列表副本。否则您的逻辑将无法正常工作。

现在您可以在ItemUpdated中创建一个事件Form2,每当在表格2中更新所选项目的值时,就引发该事件。您的主窗体(Form1)会在该事件的处理程序方法上侦听此事件,您将调用重新填充列表视图的逻辑。

因此,在创建Form2实例并为此调用.Show()时,您应该订阅它的事件。如下所示。

同样在form2中,您传递了Form1的对象,因此在从Form2移回到Form1时不必创建Form1的新实例。

在Form1中,同时调用Form2

Form2 frm = new Form2();
frm.ItemUpdated += frm_ItemUpdated;
frm.Show(this); 
this.Hide();

为此,您必须在表单1中具有frm_ItemUpdated方法。

public void frm_ItemUpdated(object sender, EventArgs e)
{
    //call your logic to updated list view
}

Form2

构造者

public event EventHandler ItemUpdated;
private Form1 form1;
private List<Product> prodList;
public Form2(Form1 sender, List<Product> productList)
{
    this.form1 = sender;
    this.prodList = productList; 
    //and other things what you are doing in contructor
}

button3在Form2上的逻辑是正确的,只需添加事件更新即可。

    private void button3_Click(object sender, EventArgs e)
    {
        //logic whatever you are doing currently

        //you should not be requred to assign updated prod list back
        //as it is referece type and change in this prodList, will refrect in main prodlist
        //frm1.productlist = prodList;

        if(ItemUpdated != null)
            ItemUpdated(this, EventArgs.Empty);
    }

并在按钮2中,返回到Form1

    private void button2_Click(object sender, EventArgs e)
    {
        //this will show the same Form1, from which we came to Form2
        frm1.Show(); 
    }