Search code examples
c#visual-studiolistboxmultiplication

How do you multiply between 2 listboxes and output to another listbox


I need to multiply the discount and amount and output the result into the discounted price an the discount will be varying every time enter image description here

` private void Discount_click(object sender, EventArgs e)
    {
        Button d = (Button)sender;
        txtPre.Text = d.Text;


        //Discount Spliitter
        var dis = txtPre.Text;
        char diseperate = '%';
        string[] displit = null;
        displit = dis.Split('%');

        string disarea = displit[0];
        lbDiscount.Items.Add("0." + disarea);

        //Discount Adder
        if (lbDiscount.Items.Count == lbAmount.Items.Count)
        {
            string strDiscount, strAmount;
            decimal discount, amount, price;
            decimal sum = 0;
            lbDiscPrice.Items.Clear()   ;
            for (int i = 0; i < lbDiscount.Items.Count; i++)
            {
                strDiscount = lbDiscount.Items[i].ToString();
                strAmount = lbAmount.Items[i].ToString();
                if (decimal.TryParse(strDiscount, out discount))
                {
                    if (decimal.TryParse(strAmount, out amount))
                    {
                        price = amount - (discount * amount);
                        lbDiscPrice.Items.Add(price);
                        sum += price;
                    }
                    else
                    {
                        MessageBox.Show("Invalid amount: " + strAmount);
                    }
                }
                else
                {
                    MessageBox.Show("Invalid discount: " + strDiscount);
                }
            }
            lblSub.Text = sum.ToString();
        }
        else
        {
            MessageBox.Show("Number of items does not match between Discount and Amount!");
        }
    }`

This is what I have right now but it doesn't seem to work.

These are what the listboxes look like with data in them, i need to multiply the

These are what the listboxes look like with data in them, i need to multiply the 3rd and 4th box together and output the result into the listbox on the far right. The discount is in a decimal form and will vary


Solution

  • If you want to do it the old fashioned way, it'd look something like:

    private void button1_Click(object sender, EventArgs e)
    {
        if (lbDiscount.Items.Count == lbAmount.Items.Count)
        {
            string strDiscount, strAmount;
            decimal discount, amount, price;
            decimal sum = 0;
            lbDiscPrice.Items.Clear();
            for(int i=0; i<lbDiscount.Items.Count; i++)
            {
                strDiscount = lbDiscount.Items[i].ToString();
                strAmount = lbAmount.Items[i].ToString();
                if (decimal.TryParse(strDiscount, out discount))
                {
                    if (decimal.TryParse(strAmount, out amount))
                    {
                        price = amount - (discount * amount);
                        lbDiscPrice.Items.Add(price);
                        sum += price;
                    }
                    else
                    {
                        MessageBox.Show("Invalid amount: " + strAmount);
                    }
                }
                else
                {
                    MessageBox.Show("Invalid discount: " + strDiscount);
                }
            }
            lblSub.Text = sum.ToString();
        }
        else
        {
            MessageBox.Show("Number of items does not match between Discount and Amount!");
        }
    }