locked
Having troubles summing up items in a listbox using a button to display the total in the label. RRS feed

  • Question

  • Hi all

    I am having trouble creating a code to add sum up all the items in the list box and then display the total in a label. My application has 5 different tabs that consist of radio button controls and a price associated with each. When the user hits the button to go to the next page, the number associated with the radio button is put into the listbox. 

    My current code is:

        {
                int count = listBoxPrice.Items.Count;
                int lastindex = count - 1;
                int index = 2;
                decimal price, total=0;
                while(index <= lastindex)
                {
                    price = decimal.Parse(listBoxPrice.Items[index].ToString());
                    total = total + price;
                    index += 1;
                }
                labelTotal.Text = "Your total is: " + "$" + total;
            }

    When I run the application I experience a handling error. 

    Sunday, December 2, 2018 6:23 PM

All replies

  •  

    My current code is:

        {
                int count = listBoxPrice.Items.Count;
                int lastindex = count - 1;
                int index = 2;
                decimal price, total=0;
                while(index <= lastindex)
                {
                    price = decimal.Parse(listBoxPrice.Items[index].ToString());
                    total = total + price;
                    index += 1;
                }
                labelTotal.Text = "Your total is: " + "$" + total;
            }

    When I run the application I experience a handling error. 

    What is the *exact* error message?

    Note that it is often preferable to use TryParse instead of Parse.

    If Parse encounters a string that does not represent a valid value for the
    associated type (such as Decimal) then it will throw an exception.

    With TryParse no exception is thrown and you can check to see if the
    conversion was successful or not.

    //price = decimal.Parse(listBoxPrice.Items[index].ToString());
    bool res = Decimal.TryParse(listBoxPrice.Items[index].ToString(), out price);
    

    If res is true then the conversion succeeded. If it's false then the
    conversion failed and price will be zero.

    - Wayne

    Sunday, December 2, 2018 8:57 PM
  • Hello,

    If what you are doing is learning then the following does not teach basics but instead is for a solution for an application.

    using System;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace StringArrayToIntArray
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
    
                var intItems = Array.ConvertAll(listBox1.Items.Cast<string>().ToArray(), (invalue => {
                    int.TryParse(invalue, out var value);
                    return value;
                }));
    
                label1.Text = $"Sum of items in listbox {intItems.Sum()}";
    
            }
       
        }
    }

    MSDN Docs Converter<TInput,TOutput> Delegate

    Here is your's modified.

    private void button2_Click(object sender, EventArgs e)
    {
        int count = listBoxPrice.Items.Count;
        int lastindex = count - 1;
        int index = 2;
        decimal price, total = 0;
        while (index <= lastindex)
        {
            decimal.TryParse(listBoxPrice.Items[index].ToString(), out var value);
            total = total + value;
            index += 1;
        }
    
        label1.Text = $"Sum of items in listbox {total}";
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Sunday, December 2, 2018 11:21 PM
  • Hi bert1212,

    Thank you for posting here.

    For your question, if the items in the listBox are not all numbers, please try the suggestions by Kareninstructor to use TryParse.

    I test your code, if you want to sum the items in the listBox and display the total, please change the code below.

    Change: 

     int index = 2;

    To:

     int index = 0;

    If this could not solve your question, please provide the details about your handing error.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Wednesday, December 12, 2018 8:50 AM
    Monday, December 3, 2018 7:53 AM