我为会计师创建了一个程序。我有按钮添加产品,删除产品,添加产品数量,销售产品和利润(从列表中的所有产品计算利润)和产品列表视图产品,数量, 购买价格和利润。我有按钮“添加产品数量”的问题我的想法是,当我在列表视图中选择产品而不是点击按钮“添加产品数量”我想打开一个新窗口(新表格)并传递所有值从列表视图中的选定项目到第二种形式,使用该值进行数学计算,然后将新值传递给第一种形式的选定项目,即更新列表视图中的选择项目。我怎么解决这个问题? 这是我的代码:
Form1中:
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();
}
答案 0 :(得分:1)
当您只谈论几个文本框时,一种简单的方法是在Form2上创建几个属性来保存列表视图中的值。
//this will display the values from the listview
textBox1.Text = firstValue;
textBox2.Text = secondValue;
在form_load事件中
//this puts the changes back into the property
firstValue = textBox1.Text;
secondValue = textBox2.Text;
在你设置为dialogresult.OK
的buttonClick事件中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;
}
现在,所有这一切的关键是您需要在Form1中填写此信息
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;
}
}
已移除代码 - 新代码涵盖:
图片已删除
以下是我使用的代码
FORM1
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;
}
}
FORM3:
<Link to={address} onClick={actions.handlerFunction}>