C# 使用其他表单c中的文本框更新listviewitems
我有一个Form1主表单,我在列表视图中加载一个在Form2中创建的产品列表。我创建了一个上下文菜单条,当我从listView中选择一行,并在该上下文菜单中选择Edit选项时,Form2显示出来,我用于创建产品的所有文本框都使用我在listView中选择的行中包含的值完成 我想做的是能够使用Form2中的文本框编辑所选行的一个或多个值,然后将更新后的产品列表发送回Form1以显示,而不改变行的顺序,就像我第一次删除该行,然后再次添加该行,但进行了更新一样 谢谢。这就是我到目前为止在Form2中所取得的成绩:C# 使用其他表单c中的文本框更新listviewitems,c#,winforms,listview,textbox,C#,Winforms,Listview,Textbox,我有一个Form1主表单,我在列表视图中加载一个在Form2中创建的产品列表。我创建了一个上下文菜单条,当我从listView中选择一行,并在该上下文菜单中选择Edit选项时,Form2显示出来,我用于创建产品的所有文本框都使用我在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的产品列表填充Form1中的listView:
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);
}
}
我假设在Form2中,您正在根据列表视图中的选定项设置textBox1__id的文本。并且不可编辑如果是,则必须将其设置为不可编辑
产品列表在两个表单中都可以访问,请确保两个表单之间都有产品列表的公共共享副本。否则你的逻辑就行不通了
现在您可以创建一个事件,比如在Form2中更新ItemUpdated,并且无论何时在Form2中更新所选item的值,都会引发该事件。此事件将由主窗体Form1在调用重新填充列表视图逻辑的事件处理程序方法上侦听
因此,在创建Form2实例并为此调用.Show时,您应该订阅它的事件。如下图所示
同样在form2中,您传递Form1的对象,所以您不必在从form2移回Form1时创建Form1的新实例
在Form1中,调用Form2时
为此,您必须在表格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
}
在按钮2中,返回表单1
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调用什么来更新我在listview中的项目?@HatzJon您尝试过这些更改了吗。请看,当您从Form2引发一个事件时,Form1正在侦听该事件,Form2不需要做更多的事情。。现在,Form1的职责是调用逻辑来完成任务。。在这种情况下。。无论您在button2中做什么,请在Form1中单击,将其仅放在Form1中的方法int中,并从Form1中的frm_ItemUpdated调用该方法
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);
}
private void button2_Click(object sender, EventArgs e)
{
//this will show the same Form1, from which we came to Form2
frm1.Show();
}