繁体   English   中英

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

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

我正在使用btnDelete_Click删除列表框中的项目,但总成本没有扣除。 请帮忙。 这是我的完整代码,我的问题开始于btnDelete_Click及以下。

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

你的问题在这里:

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

你为什么只需要一个switch来执行一个案例? 并且还可以理解, y开关变量是用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());
}

暂无
暂无

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

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