简体   繁体   English

从列表框中删除项目后如何删除和扣除总费用?

[英]How to remove and deduct total cost after removing an item from listbox?

I'm using btnDelete_Click to delete an item in my list box but the total cost didn't deducted. 我正在使用btnDelete_Click删除列表框中的项目,但总成本没有扣除。 Please help. 请帮忙。 here is my full code my problem started on btnDelete_Click and below. 这是我的完整代码,我的问题开始于btnDelete_Click及以下。

public partial class POS : Form { int totalcost; public partial class POS:Form {int totalcost; int totalremove; 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());
    }
}  
}

Your issue is here : 你的问题在这里:

 int y = 0;
 switch (y)
 {
     case 1:
         totalremove = totalcost - 200;
         break;
 }

Why do you need a switch their for executing only one case? 你为什么只需要一个switch来执行一个案例? and also it is understood that y the switch variable is initialized with 0 and in the immediate next line switch is evaluation with only single case that is 1 then how it produces the expected result? 并且还可以理解, y开关变量是用0初始化的,并且在紧接的下一行开关是评估,只有单个情况是1然后它是如何产生预期结果的?

The actual thing that you have to do, more with delete operations, HAve to get the sub total of the items that are going to be deleted and subtract that values from the actual total. 您需要做的实际操作,更多的是删除操作,有必要获取将要删除的项的子总数,并从实际总数中减去该值。 that would be the new total. 这将是新的总数。 To get the item total you have take the value by processing the item text, so the code in the click of delete button is: 要获得项目总数,您可以通过处理项目文本来获取值,因此单击“删除”按钮中的代码为:

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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM