Maths in listbox

dave_ie

Member
Joined
Mar 27, 2010
Messages
24
Programming Experience
Beginner
I am trying to do some maths. inside my List Box but its not working.

As the Code stands its only Adding the newest item and outputting the total, and if i remove the item the Total goes to 0 with 3 or more items still in the List box

What i am looking for is the Program to keep updating the Total, as the user keeps adding in Items
VB.NET:
 If Val(txtCount.Text) <= 0 Then
            MessageBox.Show("Quantity must be Added!", "Error Message")
        Else
            Dim Total As Decimal

            'Add how many is needed to Item Quantity
            Total = intprice(ComboBox1.SelectedIndex) * Val(txtCount.Text)
            Total = Total + decTax_RATE

            'Outputting Items 
            ListBox1.Items.Add(ComboBox1.Text & "       " & "     " & txtCount.Text & "     " & "       " & "     " & "@" & "       " & "€" & intprice(ComboBox1.SelectedIndex).ToString & "     " & "Ready To Ship")

        End If
 
Ok, if i use 2 list box one for Price and 1 for Topic do i need a Como Box then ?
and if so, if a User clicks on a Dvd will the price in List Price Highlighted?

IF i want to keep the Price in an Array within the Combo box, how would i use a Substring Method to get the price to add?

Some thing like this?
VB.NET:
For each Item As String in ListBox1.Items

    If Item.Contains("item name") Then

Dim ItemData() As String = Item.Split(" "c)

Dim ItemName As String = ItemData(0).Trim(" "c)

Dim ItemQuantity As Double = CDbl(ItemData(1).Trim(" "c))
 
for substring, it is very bad, it is very dirty and i will not encourage u to do it, nor will i tell u how to do it..

anyway i have already done what u shld be doing

qz1z80.jpg


Quantity textbox works the same way
Maybe if u want u can have a listbox3 for quantity
When u click btnAdd
VB.NET:
  lstTitle.Items.Add(ComboBox1.SelectedItem.ToString() & "Ready To Ship")
lstQty.Items.Add(txtCount.Text)
            lstPrice.Items.Add(intprice(ComboBox1.SelectedIndex).ToString())


Your Sum will then look like
VB.NET:
Private Function listsum() as decimal
        Dim i As Integer
        Dim sum As decimal = 0
        For i = 0 To listbox1.Items.Count - 1
            sum = sum + cdec(listbox2.items(i).tostring()) * cdec(listbox3.items(i).tostring())
        Next i
        return sum
    End Function

lblCost.Text = "Total Cost inc. VAT & Delivery " & sum.ToString("C")

for ur remove, just do a

listbox1.remove(listbox1.selectedindex)
listbox2.remove(listbox2.selectedindex)
listbox3.remove(listbox3.selectedindex)

then do a listsum again

to enable ur listbox to be selected simulataneously

On listbox1_selectedindexchanged method
VB.NET:
 listbox2.selectedindex = listbox1.selectedindex
listbox3.selectedindex = listbox1.selectedindex

the same for listbox2 and listbox3 selectedindexchanged methods, but remember to change to the correct listboxes around
 
Last edited:
Ah i see, i could'nt really picture what the 3 List box would be like within the Program, thanks alot.

As this Adding Items within One Listbox was really starting to make me go off my game, your a life saver.
 
Back
Top