我知道有很多类似的问题,但是我已经尝试过了,但它仍然没有用。所以我有一个表格,你选择一些杂货。它们已经以该形式添加到列表框(bill)中,并且计算了杂货的总和并以标签显示。通过单击按钮继续 Form1正在打开另一个列表框调用的帐单。我想在那个表格上显示来自form2和总价的杂货。所以这是我的代码
窗体2:
private void Foodanddrink_Click(object sender, EventArgs e)
{
this.Hide();
Form1 frm = new Form1(total3.text,bill.Items);
frm.ShowDialog();
}
Form1中
public Form1()
{
InitializeComponent();
}
public Form1(String total, ListBox.ObjectCollection items)
{
InitializeComponent();
bill.Items.AddRange(items);
total2.Text = total;
}
注意:有时我只需要调用Form1而不从Form2传递列表框。
任何帮助都将不胜感激。
编辑:Form2
private void NormalProjectionFamily_Click(object sender, EventArgs e)
{
pricelabel.Visible = true;
ListViewItem lvi = new ListViewItem("REgular projection");
lvi.SubItems.Add("4KM");
bill.Items.Add(lvi);
totalbill = totalbill + NormalFamily;
total3.Text = Convert.ToString(totalbill);
}
private void ThreeDProjectionFamily_Click(object sender, EventArgs e)
{
pricelabel.Visible = true;
ListViewItem lvi = new ListViewItem("3D Projection");
lvi.SubItems.Add("5KM");
bill.Items.Add(lvi);
totalbill = totalbill + ThreeDFamily;
total3.Text = Convert.ToString(totalbill);
}
private void delete_Click(object sender, EventArgs e)
{
if (bill.SelectedItems.Count == 0) return;
string currentprice = bill.SelectedItems[0].SubItems[1].Text.ToString();
double currentpricedoub = Convert.ToDouble(currentprice.Split('K')[0]);
totalbill = totalbill - currentpricedoub;
total3.Text = Convert.ToString(totalbill);
try
{
while (bill.SelectedItems.Count > 0)
{
bill.Items.Remove(bill.SelectedItems[0]);
}
}
catch { }
}
private void Foodanddrink_Click(object sender, EventArgs e)
{
this.Hide();
Form1 frm = new Form1(total3.Text,bill.Items);
frm.ShowDialog();
}
如果我试图通过总价格它完美的工作,但如果我添加列表项我有错误,如:无法从'System.Windows.Forms.ListBox.ObjectCollection'转换为'System.Windows.Forms .ListViewItem []'
或参数2:无法从'System.Windows.Forms.ListView.ListViewItemCollection'转换为'System.Windows.Forms.ListBox.ObjectCollection'