如何在 C# 的 Windows 窗体应用程序中将项目从主窗体中的列表视图更新为第二个窗体中的文本框
本文关键字:窗体 更新 视图 列表 文本 第二个 Windows 应用程序 项目 | 更新日期: 2023-09-27 18:33:59
我为会计师创建了一个程序。我有用于添加产品,删除产品,添加产品数量,销售产品和利润(计算列表中所有产品的利润)和带有产品,数量,列的产品列表视图,购买价格和利润。我对按钮"添加产品数量"有问题,我的想法是,当我在列表视图中选择产品并单击"添加产品数量"按钮时,我想打开一个新窗口(新表单)并将所选项目中的所有值从列表视图传递到第二形式,使用该值进行数学计算,然后将该新值传递给第一个形式的选定项目,即更新选择项目列表视图。我该如何解决这个问题?这是我的代码:
表格1:
public int Index;
private void button3_Click(object sender, EventArgs e)
{
Form3 form3 = new Form3();
form3.Show();
form3.Hide();
if (listView1.SelectedItems.Count > 0)
{
this.listView1.Items[0].Focused = true;
this.listView1.Items[0].Selected = true;
this.Index = listView1.FocusedItem.Index;
ListViewItem selectedItem = listView1.SelectedItems[0];
form3.GetData(listView1.Items[listView1.FocusedItem.Index].SubItems[0].Text, listView1.Items[listView1.FocusedItem.Index].SubItems[1].Text,
listView1.Items[listView1.FocusedItem.Index].SubItems[2].Text, listView1.Items[listView1.FocusedItem.Index].SubItems[3].Text);
DialogResult ans = form3.ShowDialog();
if (ans == DialogResult.OK)
{
listView1.FocusedItem.SubItems[0].Text = form3.Prozz;
listView1.FocusedItem.SubItems[1].Text = form3.Kolii;
listView1.FocusedItem.SubItems[2].Text = form3.Cenaa;
listView1.FocusedItem.SubItems[3].Text = form3.Proff;
}
}
}
表格2:
public string Prozz { get; set; }
public string Kolii { get; set; }
public string Cenaa { get; set; }
public string Proff { get; set; }
public Form3()
{
InitializeComponent();
}
public void GetData(string Proi, string Kol, string Cena, string Profit)
{
textBox1.Text = Kol;
textBox2.Text = Cena;
textBox3.Text = Profit;
textBox6.Text = Proi;
}
public void SetData(string Proz, string Kol, string NabCena, string Prof)
{
int x = int.Parse(Kol);
double y = double.Parse(NabCena);
double z = double.Parse(Prof);
int a = int.Parse(textBox4.Text);
double b = double.Parse(textBox5.Text);
int Kolicina = x + a;
double sumNabavnaCena = (x * y + a * b) / (x + a);
double Profit = z - Kolicina * sumNabavnaCena;
Prozz = Convert.ToString(Proz);
Kolii = Convert.ToString(Kolicina);
Cenaa = Convert.ToString(sumNabavnaCena);
Proff = Convert.ToString(Profit);
}
private void button1_Click(object sender, EventArgs e)
{
SetData(textBox6.Text, textBox1.Text, textBox2.Text, textBox3.Text);
this.button1.Text = "OK";
this.button1.DialogResult = DialogResult.OK;
this.Close();
}
当您只谈论几个文本框时,执行此操作的一种简单方法是在 Form2 上创建几个属性来保存列表视图中的值。
public string firstValue {get; set;}
public string secondValue {get; set;}
在您的form_load活动中
//this will display the values from the listview
textBox1.Text = firstValue;
textBox2.Text = secondValue;
在按钮单击事件中,您已设置为对话框结果。还行
//this puts the changes back into the property
firstValue = textBox1.Text;
secondValue = textBox2.Text;
现在,所有这些的关键是您需要在Form1中填写此信息
var f2 as new Form2();
//fill the firstValue propery with the 3rd column of the listview
f2.firstValue = listview1.FocusedItem.SubItems[2].Text;
//fill the secondValue property with the 4th column of the listview
f2.secondValue = listview1.FocusedItem.SubItems[3].Text;
//show f2 as a dialog
DialogResult ans = f2.ShowDialog();
if(ans == DialogResult.OK)
{
//update the listview with the values from the properties of form2
listview1.focusedItem.SubItems[2].Text = f2.firstValue;
listview1.FocusedItem.SubItems[3].Text = f2.secondValue;
}
代码已删除 - 新代码涵盖代码:
图像已删除
这是我使用
的代码表格1
public partial class Form1 : Form
{
Form3 form3 = null;
public Form1()
{
InitializeComponent();
}
/* private void Form1_Load(object sender, EventArgs e)
{
//You won't need this. It just populates my
//listview with some Dummy Data. I'll leave
//it here in case you want to mess with it
for (int i = 0; i < 10; i++)
{
var lv = new ListViewItem();
lv.Text = "Item" + i;
lv.SubItems.Add((6+ i).ToString());
lv.SubItems.Add((i + 7).ToString());
lv.SubItems.Add((i + 3).ToString());
listView1.Items.Add(lv);
}
}*/
//public int Index; //not needed
private void button3_Click(object sender, EventArgs e)
{
form3 = new Form3();
if (listView1.SelectedItems.Count > 0)
{
//this.listView1.Items[0].Focused = true;
//this.listView1.Items[0].Selected = true;
//this.Index = listView1.FocusedItem.Index;
//ListViewItem selecteditem = listView1.SelectedItems[0];
form3.GetData(listView1.FocusedItem.Text, listView1.FocusedItem.SubItems[1].Text,
listView1.FocusedItem.SubItems[2].Text, listView1.FocusedItem.SubItems[3].Text);
DialogResult ans = form3.ShowDialog();
if (ans == DialogResult.OK)
{
listView1.FocusedItem.Text = form3.Prozz;
listView1.FocusedItem.SubItems[1].Text = form3.Kolii;
listView1.FocusedItem.SubItems[2].Text = form3.Cenaa;
listView1.FocusedItem.SubItems[3].Text = form3.Proff;
}
}
form3 = null;
}
}
表格3:
public partial class Form3 : Form
{
public string Prozz { get; set; }
public string Kolii { get; set; }
public string Cenaa { get; set; }
public string Proff { get; set; }
public Form3()
{
InitializeComponent();
}
public void GetData(string Proi, string Kol, string Cena, string Profit)
{
textBox1.Text = Kol;
textBox2.Text = Cena;
textBox3.Text = Profit;
textBox6.Text = Proi;
}
public void SetData(string Proz, string Kol, string NabCena, string Prof)
{
int x = int.Parse(Kol);
double y = double.Parse(NabCena);
double z = double.Parse(Prof);
int a = int.Parse(textBox4.Text);
double b = double.Parse(textBox5.Text);
int Kolicna = x + a;
double subNabavnaCena = (x * y + a * b) / (x + a);
double Profit = z - Kolicna * subNabavnaCena;
Prozz = Proz.ToString();
Kolii = Kolicna.ToString();
Cenaa = subNabavnaCena.ToString();
Proff = Profit.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
SetData(textBox6.Text, textBox1.Text, textBox2.Text, textBox3.Text);
//you should move these two lines up under InitializeComponent()
//otherwise you will never see the button text set to OK and you
//can set the dialogResult before you need the button.
this.button1.Text = "OK";
this.button1.DialogResult = DialogResult.OK;
}
}