none
Summing up all decimal value in ListBox using C#

    Question

  • Hi

    I am a newbie to C#. How can I sum up all decimal values in a ListBox? The ListBox contain all decimal values.

    For example, if my ListBox contain 2.11, 123.456 and ........., how can I add them up using C#?

    Can anyone help? Urgent

    • Moved by kleinmaMVP, Moderator Thursday, May 10, 2012 11:18 PM C# question (From:Visual Basic Language)
    Thursday, May 10, 2012 5:36 PM

Answers

  • This works so long as you can be sure the values in the listbox are always numeric.

                decimal sumOfListbox =
                        (from string S in listBox1.Items
                         select Convert.ToDecimal(S))
                        .Sum();
    
                MessageBox.Show(sumOfListbox.ToString());


    Matt Kleinwaks - MSMVP MSDN Forums Moderator - www.zerosandtheone.com

    Thursday, May 10, 2012 11:27 PM

All replies

  • Hello LoLhc,

    Hi

    I am a newbie to C#. How can I sum up all decimal values in a ListBox? The ListBox contain all decimal values.

    For example, if my ListBox contain 2.11, 123.456 and ........., how can I add them up using C#?

    Can anyone help? Urgent

    Welcome to Msdn Forum , thi is Forum for VisualBasic , you can ask your question in dedicated Forum for C# http://social.msdn.microsoft.com/Forums/it-IT/Vsexpressvcs/threads

    Regards.


    Thursday, May 10, 2012 6:31 PM
  • This works so long as you can be sure the values in the listbox are always numeric.

                decimal sumOfListbox =
                        (from string S in listBox1.Items
                         select Convert.ToDecimal(S))
                        .Sum();
    
                MessageBox.Show(sumOfListbox.ToString());


    Matt Kleinwaks - MSMVP MSDN Forums Moderator - www.zerosandtheone.com

    Thursday, May 10, 2012 11:27 PM