【问题标题】:Update listviewitems using textboxes in another form c#使用另一种形式的文本框更新listviewitems c#
【发布时间】:2018-11-30 04:50:41
【问题描述】:

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

我想要做的是能够编辑使用 Form2 中的文本框选择的行的一个或多个值,然后将更新的产品列表发送回 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);

            }
        }

【问题讨论】:

    标签: c# winforms listview textbox


    【解决方案1】:

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

    产品列表可通过两种形式访问,请确保您在两种形式之间都有产品列表的通用(共享)副本。否则你的逻辑将不起作用。

    现在您可以在Form2 中创建一个事件,比如ItemUpdated,并且每当您在表格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
    }
    

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

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

    【讨论】:

    • 好吧,但是在 Form1 的 ItemUpdated 中,我应该从 Form2 调用什么来更新列表视图中的项目?
    • @HatzJon 你试过这个改变吗?看看当你从 Form2 中引发一个事件时,Form1 正在监听,Form2 不需要做更多的事情。现在 Form1 有责任调用逻辑来做事情。在这种情况下,无论你在做什么在 Form1 中的 button2_Click 中,将其放入方法中(仅限 int form 1),并从 frm_ItemUpdated 中调用该方法(也在 Form1 中)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-09
    • 1970-01-01
    • 2016-03-07
    • 1970-01-01
    相关资源
    最近更新 更多