简体   繁体   中英

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. Please help. here is my full code my problem started on btnDelete_Click and below.

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

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? 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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