Results 1 to 7 of 7

Thread: [RESOLVED] Sum Listbox

  1. #1

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Resolved [RESOLVED] Sum Listbox

    I have tried both of these to sum a listbox's entries
    Code:
            Dim number As Integer = Me.lstBracingLengths.Items.Count - 1
            Dim y As Integer
            Dim totall As Integer
            For y = 0 To number
                totall = totall + Convert.ToInt16(Me.lstBracingLengths.Items.Item(y))
            Next y
            Me.txtBracingTotals.Text = Convert.ToString(totall)
    Code:
            Dim totall As Integer
            For Each item As Integer In lstBracingLengths.Items
                totall += item
            Next
            txtBracingTotals.Text = totall
    Both work but they round the values up for some reason i cannot see.
    see picture


    I am using a round up formula before the entries are placed in the listbox but i cannot see that causing it
    Code:
            Dim wl As Double = Me.txtWallLength.Text
            Dim wh As Double = Me.cmbWallHeight.Text
            Dim sqr1 As Double = wl * wl
            Dim sqr2 As Double = wh * wh
            Dim Total As Double = sqr1 + sqr2
            Dim sqrTotal As Double = Math.Sqrt(Total)
            'populates listbox with bracing lengths
            Me.lstBracingLengths.Items.Add(Math.Round(sqrTotal, 3))
    regards
    toe

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Sum Listbox

    tis because Convert.ToInt16(5.564) = 6
    kevin
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: Sum Listbox

    Quote Originally Posted by kebo
    tis because Convert.ToInt16(5.564) = 6
    kevin
    thanks for the quick reply kebo.
    It also happins when using the 2nd block of code which dosnt have Convert.ToInt16?
    regards
    toe

  4. #4
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Sum Listbox

    yea...its because you are converting to a integer still

    Code:
    For Each item As Integer In lstBracingLengths.Items
    BTW....you don't seem to have Option Strict turned on...if you did you wouldn't be able to run that line of code you did because you are turning ListBoxItems into integer

    try this
    vb Code:
    1. For Each Item as ListViewItem in lstBracingLengths.Items
    2.      totall += CType(item.Text, Double)
    3. Next
    4. txtBracingTotals.Text = totall
    kevin
    Last edited by kebo; Sep 8th, 2007 at 06:55 PM. Reason: typos....
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Sum Listbox

    Did you add Double values to the ListBox in the first place? If so then this:
    vb.net Code:
    1. Dim totall As Integer
    2.         For Each item As Integer In lstBracingLengths.Items
    3.             totall += item
    4.         Next
    5.         txtBracingTotals.Text = totall
    is the most appropriate way to do it but, as kebo said, if you cast as type Integer then of course the fractional parts will be rounded. It should be:
    vb.net Code:
    1. Dim totall As Double
    2.         For Each item As Double In lstBracingLengths.Items
    3.             totall += item
    4.         Next
    5.         txtBracingTotals.Text = totall
    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

  6. #6

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: Sum Listbox

    Thanks Guys.
    Did you add Double values to the ListBox in the first place? Yes, values as in the listbox picture above using this code
    Code:
            Dim wl As Double = Me.txtWallLength.Text
            Dim wh As Double = Me.cmbWallHeight.Text
            Dim sqr1 As Double = wl * wl
            Dim sqr2 As Double = wh * wh
            Dim Total As Double = sqr1 + sqr2
            Dim sqrTotal As Double = Math.Sqrt(Total)
            'populates listbox with bracing lengths
            Me.lstBracingLengths.Items.Add(Math.Round(sqrTotal, 3))
    Your second code works correctly jmcilhinney
    Code:
            Dim totall As Double
            For Each item As Double In lstBracingLengths.Items
                totall += item
            Next
            txtBracingTotals.Text = totall
    Option Explicit was on kevin.

    I really appreciate your effort in helping me.
    regards
    toe

  7. #7
    New Member
    Join Date
    Nov 2007
    Posts
    1

    Re: [RESOLVED] Sum Listbox

    im new here, i had this same problem and this has help me a lot thank you very much

Posting Permissions

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



Click Here to Expand Forum to Full Width