簡體   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