我使用btnDelete_Click删除列表框中的项目,但总费用没有扣除。请帮忙。这是我的完整代码 我的问题开始于btnDelete_Click及以下。
公共部分类POS:表格 { int totalcost; int totalremove;
public POS()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
String[] Juice;
Juice = new String[10];
Juice[0] = "Baby's Milk";
Juice[1] = "Pine Apple";
Juice[2] = "Vampire Venom";
Double[] Prices;
Prices = new Double[10];
Prices[0] = 200;
Prices[1] = 250;
Prices[2] = 300;
for (int i = 0; i < 4; i++)
{
lstBoxProducts.Items.Add(Juice[i] + "-" + Prices[i]);
}
}
private void btnAdd_Click_1(object sender, EventArgs e)
{
string text = lstBoxProducts.GetItemText(lstBoxProducts.SelectedItem);
lstProductChosen.Items.Add(text);
int x = 0;
if (lstBoxProducts.SelectedIndex == 0)
{
x = x + 1;
}
else if (lstBoxProducts.SelectedIndex == 1)
{
x = x + 2;
}
else if (lstBoxProducts.SelectedIndex == 2)
{
x = x + 3;
}
switch (x)
{
case 1:
totalcost = totalcost + 200;
break;
case 2:
totalcost = totalcost + 250;
break;
case 3:
totalcost = totalcost + 300;
break;
}
lbTotalCost.Text = ("Php" + totalcost.ToString());
}
private void POS_Load(object sender, EventArgs e)
{
}
private void btnDelete_Click(object sender, EventArgs e)
{
for (int i = lstProductChosen.SelectedIndices.Count - 1; i >= 0; i--)
{
lstProductChosen.Items.RemoveAt(lstProductChosen.SelectedIndices[i]);
}
int y = 0;
switch (y)
{
case 1:
totalremove = totalcost - 200;
break;
}
lbTotalCost.Text = ("Php" + totalcost.ToString());
}
}
}
答案 0 :(得分:0)
您的问题在这里:
int y = 0;
switch (y)
{
case 1:
totalremove = totalcost - 200;
break;
}
为什么只需要一个switch
来执行一个案例?并且可以理解,y
switch变量是用0
初始化的,并且在下一行中,切换是评估只有单个案例1
然后它是如何产生预期结果的?
你需要做的实际操作,更多的是使用删除操作,可以获得要删除的项的子总数,并从实际总数中减去这些值。这将是新的总数。要获得项目总数,您可以通过处理项目文本来获取值,因此单击“删除”按钮中的代码为:
private void btnDelete_Click(object sender, EventArgs e)
{
int deleteCost = 0;
int itemCost = 0;
foreach(int selectedIndex in lstProductChosen.SelectedIndices)
{
itemCost = int.Parse(lstProductChosen.Items[selectedIndex].ToString().Split('-')[1]);
deleteCost += itemCost; lstProductChosen.Items.RemoveAt(selectedIndex);
}
totalcost = totalcost - deleteCost;
lbTotalCost.Text = ("Php" + totalcost.ToString());
}