|
-
Sep 8th, 2007, 06:12 PM
#1
Thread Starter
Frenzied Member
[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
-
Sep 8th, 2007, 06:26 PM
#2
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
-
Sep 8th, 2007, 06:32 PM
#3
Thread Starter
Frenzied Member
Re: Sum Listbox
 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
-
Sep 8th, 2007, 06:39 PM
#4
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:
For Each Item as ListViewItem in lstBracingLengths.Items
totall += CType(item.Text, Double)
Next
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
-
Sep 8th, 2007, 08:32 PM
#5
Re: Sum Listbox
Did you add Double values to the ListBox in the first place? If so then this:
vb.net Code:
Dim totall As Integer
For Each item As Integer In lstBracingLengths.Items
totall += item
Next
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:
Dim totall As Double
For Each item As Double In lstBracingLengths.Items
totall += item
Next
txtBracingTotals.Text = totall
-
Sep 8th, 2007, 09:03 PM
#6
Thread Starter
Frenzied Member
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
-
Nov 13th, 2007, 04:38 PM
#7
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|