使用另一种形式的文本框更新 listviewitems c#
Update listviewitems using textboxes in another form c#
我有 Form1(主窗体),我在 ListView 中加载了在 Form2 中创建的产品列表。我创建了一个上下文菜单条,当我 select 来自 listView 的一行,并在该上下文菜单中选择“编辑”选项时,Form2 出现,我用来创建产品的所有文本框都使用包含的值完成在我在 ListView 中 selected 的行中。
我想要做的是能够使用 Form2 中的文本框编辑 selected 行的一个或多个值,然后将更新后的产品列表发送回 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);
}
}
我假设您在 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
}
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();
}
我有 Form1(主窗体),我在 ListView 中加载了在 Form2 中创建的产品列表。我创建了一个上下文菜单条,当我 select 来自 listView 的一行,并在该上下文菜单中选择“编辑”选项时,Form2 出现,我用来创建产品的所有文本框都使用包含的值完成在我在 ListView 中 selected 的行中。
我想要做的是能够使用 Form2 中的文本框编辑 selected 行的一个或多个值,然后将更新后的产品列表发送回 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);
}
}
我假设您在 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
}
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();
}