Calculating Total from a listbox into a label.-VBForums
Results 1 to 3 of 3

Thread: Calculating Total from a listbox into a label.

  1. #1

    Thread Starter
    Junior Member VBAppleCoder's Avatar
    Join Date
    Oct 2017
    Posts
    26

    Calculating Total from a listbox into a label.

    In my program I have a “Calculate Total” button that when clicked calculates the total of the items in a list box. I have it coded where I get the right number into the label when the button is clicked but it is not in currency. I need the number to be a dollar amount.

    My code is:

    Dim sum As Integer
    For x As Integer = 0 To listCosts.Items.Count - 1
    sum += CDbl(lstCosts.Items(x))
    Next
    lblTotal.Text = sum.ToString

  2. #2

    Thread Starter
    Junior Member VBAppleCoder's Avatar
    Join Date
    Oct 2017
    Posts
    26

    Re: Calculating Total from a listbox into a label.

    Actually just figured it out! On lblTotal.Text = sum.ToString I needed to add currency to the end so it’s

    lblTotal.Text = sum.ToString(“c”)

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,097

    Re: Calculating Total from a listbox into a label.

    The ToString method you are calling is overloaded and allows you to pass a format specifier. You can specify currency among numerous other formats.

    https://social.msdn.microsoft.com/Se...mark=true&ac=2
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.