Help with sum listbox

Rumrunner

New member
Joined
Jul 18, 2007
Messages
2
Programming Experience
Beginner
Hello all... I am new to VB and would appreciate any help with something. I created 3 list boxes. I have 2 buttons, one button to transfer a value from the 1st and 2nd boxes into the 3d box. That part I'm fine with, just used If/else and also did it with case statements. The part I'm stumped on is my 2nd button is to add all the values in the 3d list box into a label (total).

I tried a For loop, Do While, While - Items.Add, SelectedItem/Index, Items.Count, etc.. I came close a few times... My variable is called dblAdded which is the items in the 3d list box. I tried dblTotal += dblAdded in the loop. I came close a few times, but they won't add correctly.

Any help would be greatly appreciated, Thx..
 
I think this is what you are trying to do,

VB.NET:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim sum As Double
        For Each item As String In Me.ListBox3.SelectedItems
            sum += Double.Parse(item)
        Next
        Me.Label1.Text = sum
    End Sub
 
Thanks for the suggestions I almost got it.. I didn't even think to parse. I am using now:

VB.NET:
Dim sum As Double
        For Each dblAdded As String In Me.lstCosts.SelectedItems
            sum += Double.Parse(dblAdded)
        Next
        lblTotal.Text = FormatCurrency(sum)

It is adding 2 values now, but not adding more than two. In my 3d list box if I have say 5 values, it adds the last two...
 
If you use
VB.NET:
Me.lstCosts.SelectedItems
you will get only the selected items. To get all the items of 3d listbox use
VB.NET:
Me.lstCosts.Items
 
from what i can tell it's almost like the guys wants to add all the values in one listbox based on the selected items from a 2nd listbox

it's hard to tell
 
Back
Top