按钮1-10是项目;the pos screenshot
我使用过但无法使用的代码,也许是错误的,但我无法在互联网上找到答案
private void enterPayment_Click(object sender, EventArgs e)
{
label1.Text = "Payment";
//price.ReadOnly = false;
//price.Text = "0.00";
//price.Focus();
//Kukunin ko yung total ng List Items
double total = 0;
int ilan = orders.Items.Count;
for (int i = 0; i >= ilan; i++)
{
string item = orders.Items[i].ToString();
int Index = item.IndexOf("@");
int Length = item.Length;
string presyoString = item.Substring(Index + 1, Length - Index - 1);
double presyoDouble = double.Parse(presyoString);
total += presyoDouble;
//price.Text = (total + ".00");
}
price.Text = (total + ".00");
}
我强烈建议您仅将列表框用作视图,而不要用于执行数学运算。可以在列表等集合中使用该数据,以便执行更好的操作。
例如,在程序加载时,我将在List<T>
和表单上添加产品信息,我在按钮中放置了一个标签,将其视为产品ID。因此,当我单击按钮时,它将传递tag属性,然后从那里,我将在列表中搜索有关我的ID的产品信息,并将其添加到另一个最终的List<T>
中并获取其总和。
public partial class Form1 : Form
{
private List<ProductDisplay> listProductDisplay = new List<ProductDisplay>();
private List<ProductInformation> listProductInfo = new List<ProductInformation>();
public Form1()
{
InitializeComponent();
LoadProduct();
}
private void LoadProduct()
{
listProductDisplay = new List<ProductDisplay>()
{
new ProductDisplay{ProdID = 1,ProdName = "Chargrilled Burger",ProdPrice = 330.00m},
new ProductDisplay{ProdID = 2,ProdName = "Mushroom N' Swish",ProdPrice = 330.00m},
new ProductDisplay{ProdID = 3,ProdName = "Chicken Burger",ProdPrice = 250.00m},
new ProductDisplay{ProdID = 4,ProdName = "Steak Loader",ProdPrice = 220.00m},
new ProductDisplay{ProdID = 5,ProdName = "Cookie Sandwich",ProdPrice = 125.00m},
new ProductDisplay{ProdID = 6,ProdName = "Cookie Sundae",ProdPrice = 175.00m},
new ProductDisplay{ProdID = 7,ProdName = "Chicken Nuggets",ProdPrice = 145.00m},
new ProductDisplay{ProdID = 8,ProdName = "Curly Fries",ProdPrice = 75.00m},
new ProductDisplay{ProdID = 9,ProdName = "Sprite",ProdPrice = 50.00m},
new ProductDisplay{ProdID = 10,ProdName = "Coke",ProdPrice = 50.00m}
};
}
private void InsertOrder_ButtonClick(object sender, EventArgs e)
{
try
{
Button btn = (Button)sender;
int number = Convert.ToInt32(btn.Tag);
var itemProduct = listProductDisplay.First(x => x.ProdID == number);
ProductInformation prod = new ProductInformation
{
ProdID = itemProduct.ProdID,
ProdName = itemProduct.ProdName,
ProdPrice = itemProduct.ProdPrice,
ProdQty = 1
};
prod.ProdDisplayName = $"{prod.ProdQty}x {prod.ProdName} @{prod.ProdPrice.ToString("F")} = {(prod.ProdPrice * prod.ProdQty).ToString("F")}";
listProductInfo.Add(prod);
listBoxItem.DataSource = null;
listBoxItem.DataSource = listProductInfo;
listBoxItem.DisplayMember = "ProdDisplayName";
listBoxItem.ValueMember = "ProdID";
var price = listProductInfo.Sum(t => (t.ProdPrice * t.ProdQty));
txtPayment.Text = price.ToString("F");
}
catch (Exception ex)
{
MessageBox.Show("Failed to insert order");
throw;
}
}
}
public class ProductDisplay
{
public int ProdID { get; set; }
public string ProdName { get; set; }
public decimal ProdPrice { get; set; }
}
public class ProductInformation
{
public int ProdID { get; set; }
public string ProdName { get; set; }
public string ProdDisplayName { get; set; }
public decimal ProdPrice { get; set; }
public int ProdQty { get; set; }
}
您可以使用Linq和Regex计算价格。例如
var regex = new Regex(@"=\W*(?<Price>([\d.]*))");
var total = orders.Select(x=> regex.Match(x.ToString()))
.Cast<Match>()
.Sum(x=>double.Parse(x.Groups["Price"].Value));
Regex "=\W*(?<Price>([\d.]*))"
将允许您匹配=
字符后的小数部分。然后,您可以使用它来计算total
。
样本
输入
2 x Something @ 1 = 2.00
3 x Something @ 2 = 6.00
1 x Something @ 3 = 3.00
3 x Something @ 4 = 12.00
1 x Something @ 5 = 5.00
2 x Something @ 6 = 12.00
2 x Something @ 7 = 14.00
3 x Something @ 8 = 24.00
2 x Something @ 9 = 18.00
2 x Something @ 10 = 20.00
输出
116
从这样的字符串中解析和提取值非常容易出错并且很脆弱。如果您的行中的字段顺序发生变化,会发生什么?还是添加货币符号?
@ Luiey的解决方案是最明智的方法。但是,如果由于某种原因您不能对代码进行太多更改,那么您要做的最低限度就是将项目作为自定义对象存储在列表中,其中包含价格,数量和总计的静态类型字段。 ListBox类在呈现项目时调用ToString()
。因此,您可以轻松地重写此方法以根据需要准备输出字符串。
private class LineItem
{
public LineItem()
{
Quantity = 0;
Description = string.Empty;
Price = 0;
}
public int Quantity
{
get;
set;
}
public string Description
{
get;
set;
}
public int Price
{
get;
set;
}
public int Total
{
get
{
return Quantity * Price;
}
}
public override string ToString()
{
return $"{Quantity} × {Description} @ {Price} = {Total}";
}
}
然后您像这样将一个项目添加到列表框。
var item = new LineItem()
{
Quantity = 1,
Description = "Foo bar",
Price = 10
};
listBox1.Items.Add(item);
并像这样将它们加起来。
var items = listBox1.Items.Cast<LineItem>().ToArray();
var accumulator = 0;
accumulator = items.Aggregate(accumulator, (a, i) => a + (i.Quantity * i.Price), (a) => a);