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
 
I'm a newbie and a beginner to the forums so sure people who are much wiser and brighter than me will be able to come up with the correct solution to urproblem.

But having said that thinking aloud her if u need ur code to calculate multiple times would u not require a loop? U could then run the calculations on each item in the list box? I maybe sending u in completely the wrong direction but this would be where I start. Hope it gives food for thought.
 
ur problem lies with ur remove code i think

i bet when u click remove ur total is reinit to zero

i have to look at ur remove code to pinpoint the issue
 
The remove is the same as the add with - were the + are.

I don think it be the remove as when I add the items it's not adding the values insde the listbox i bet it just some thing small that I'm over looking
 
Since you are using the variable "Total" in more than one function/sub, you might try making it a global variable.;)
 
Here is my Program, i have a few issues with this which is Really doing my head in,

One, the Adding of the Items inside List Box,
two, Cant seem to get to Copy users Details and items inside List box to Save inside a Text File, I can get the Items to Save but not the Users Details
Three, My Log in Form and Setting up user form,

If some one would'nt mind running over it and telling me where im going wrong please.
 

Attachments

  • ProductPage.zip
    135.9 KB · Views: 25
Last edited by a moderator:
Some errors


Adding the item into the listbox
Whatever u are doing now is wrong, I suggest 2 listbox, 1 for cost and 1 for title

U are required to do a loop to get a total sum, with wat u are currently doing, it is very bad
VB.NET:
dim decTotal as decimal = 0
for i as integer = 0 to listbox1.count -1
    decTotal += cint( ListBox2.Items(0).ToString())
next

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

I duno where u get ur user details from

ur login form and user form, just rename ur textbox Name, Please do not name ur textbox "name", it conflicts with an existing property of the form, and so an error will definitely occur

to rename, right click on
VB.NET:
Friend WithEvents Name As System.Windows.Forms.TextBox

and rename it as txtname

To use Left / Right U have to use Microsoft.VisualBasic.Left / Right to remove the error generated
VB.NET:
If Microsoft.VisualBasic.Left(name, 1) = "." Or Microsoft.VisualBasic.Right(name, 1) = "." Then
                isitvalid = False
                Return isitvalid
                Exit Function
            End If

I think there is a conflicting function
 
for the Items Inside List box what would i need to put down Do While or do Until ?

with the two list box one with price and the other with Titles,
I need to use a Combo Box with Array for that, and only 1 list box in the Program.

The Users Details are stored in the User.txt file which is made when you Create a user.
 
Last edited:
for the Items Inside List box what would i need to put down Do While or do Until ?

with the two list box one with price and the other with Titles,
I need to use a Combo Box with Array for that, and only 1 list box in the Program.
.

Sorry, I dont understand ur question :(
 
You said it is best to place 2 list box in the program with one with price, and Header.

I have an Array, Which looks much tidy, with the Price also in an Array within the Combo Box,

With the Loop for the maths,
VB.NET:
Dim sum As Long
        sum = listsum(list)
        'decTotal = intprice(ComboBox1.SelectedIndex) * Val(txtCount.Text)
        lblCost.Text = "Total Cost inc. VAT & Delivery " & sum.ToString("C")
        txtCount.Clear()
VB.NET:
  Private Function listsum(ByVal list As ListBox)
        Dim i As Integer
        Dim sum As Long
        For i = 0 To list.Items.Count - 1
            sum = sum + Val(list.Items.Add(i)) + Val(txtCount.Text)
        Next i
        listsum = sum
    End Function
I tried this way but its giving a Value for the First Item as just 2
with 0,1,2,3 under one another, the next Item out puts value as 100 so on.
 
Sorry, please paste full code, i have trouble understanding you

I dont know where ur array come from and how ur array look like

I would say

I think u shld put it this way

ComboBox Display value should be what u want to show

Combobox Data value should be price of item

Quantity textbox works the same way

When u click btnAdd

Listbox1 should be Listbox1.items.add(combobox.selecteditem.tostring())
Listbox2 should be Listbox2.items.add(combobox.selectedvalue.tostring())

Maybe if u want u can have a listbox3 for quantity

Listbox3 should then be listbox3.items.add(txtQuantity.Tostring())

Your Sum will then look like
VB.NET:
Private Function listsum(ByVal list As ListBox)
        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 the numbers around
 
Last edited:
What about just 1 list box?

VB.NET:
Imports System.IO
Public Class Form1
    Inherits System.Windows.Forms.Form
    Const intMax_DVD As Integer = 26
    Const intMax_price As Integer = 26
    Dim intprice(intMax_price) As Integer
    Dim strCombobox1(intMax_DVD) As String
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       
      'Combo Box Array

        strCombobox1(0) = "The Hang over"
        strCombobox1(1) = "2012"
        strCombobox1(2) = "Avatar"
        strCombobox1(3) = "Up"
        strCombobox1(4) = "The Hurt Locker"
        strCombobox1(5) = "Harry Brown"
        strCombobox1(6) = "Toy Story"
        strCombobox1(7) = "Toy Story 2"
        strCombobox1(8) = "Inglourious Basterds"
        strCombobox1(9) = "A Serious man"
        strCombobox1(10) = "Sherlock Homes 2009"
        strCombobox1(11) = "Transformers 2: Revenge Of The Fallen"
        strCombobox1(12) = "Into The Wild"
        strCombobox1(13) = " Gran Torino"
        strCombobox1(14) = "Body of Lies"
        strCombobox1(15) = "300"
        strCombobox1(16) = "Lost: Complete Season 1 Box Set (5 Discs)"
        strCombobox1(17) = "Lost: Complete Season 2 Box Set (5 Discs)"
        strCombobox1(18) = "Lost: Complete Season 3 Box Set (5 Discs)"
        strCombobox1(19) = "Lost: Complete Season 4 Box Set (5 Discs)"
        strCombobox1(20) = "Lost: Complete Season 5 Box Set (5 Discs)"
        strCombobox1(21) = "House Season 1 Box Set "
        strCombobox1(22) = "House Season 2 Box Set"
        strCombobox1(23) = "House Season 3 Box Set "
        strCombobox1(24) = "Prison Break Season 1 Box Set"
        strCombobox1(25) = "Prison Break Season 2 Box Set"
        strCombobox1(26) = "Prison Break Season 3 Box Set"

        'Price of Dvd Array

        intprice(0) = "14"
        intprice(1) = "15"
        intprice(2) = "22"
        intprice(3) = "20"
        intprice(4) = "19"
        intprice(5) = "19"
        intprice(6) = "5"
        intprice(7) = "5"
        intprice(8) = "24"
        intprice(9) = "20"
        intprice(10) = "18"
        intprice(11) = "14"
        intprice(12) = "15"
        intprice(13) = "16"
        intprice(14) = "13"
        intprice(15) = "18"
        intprice(16) = "50"
        intprice(17) = "50"
        intprice(18) = "53"
        intprice(19) = "60"
        intprice(20) = "77"
        intprice(21) = "43"
        intprice(22) = "44"
        intprice(23) = "44"
        intprice(24) = "30"
        intprice(25) = "35"
        intprice(26) = "45"

    End Sub
    Private Sub btnRemove1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove1.Click

    End Sub
    Private Sub btnAdd1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd1.Click

        'Checking if txtCount is Greater then Zero
        If Val(txtCount.Text) <= 0 Then
            MessageBox.Show("Quantity must be Added!", "Error Message")
        Else

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

        Dim sum As Long
        sum = listsum(list)
        'decTotal = intprice(ComboBox1.SelectedIndex) * Val(txtCount.Text)
        lblCost.Text = "Total Cost inc. VAT & Delivery " & sum.ToString("C")
        txtCount.Clear()
    End Sub



    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        'Increasing Quantite of Dvd's 
        txtCount.Text = Val(txtCount.Text) + 1
    End Sub

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click

        'decreasing Quantite of Dvd's
        txtCount.Text = Val(txtCount.Text) - 1
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles list.SelectedIndexChanged

        'Shows What Item Clicked on inside TxtList.Text
        txtlist.Text = list.SelectedItem

        'Saving List Box to a Text file
        Dim w As IO.StreamWriter
        Dim i As Integer
        w = New IO.StreamWriter("Save.txt")
        For i = 0 To list.Items.Count - 1
            w.WriteLine(list.Items.Item(i))
        Next

        w.Close()
    End Sub

    Private Function listsum(ByVal list As ListBox)
        Dim i As Integer
        Dim sum As Long

        For i = 0 To list.Items.Count - 1
            sum = sum + Val(list.Items.Add(i)) + Val(txtCount.Text)
        Next i
        listsum = sum

    End Function
End Class

Here is a Screen Shot Of the Program Running,
As you can See Toy Story 2 should be 5 but on out put it showed 1. and the other 2, and the total is 6.00
Where is should be Total = 36.00, At Lest i getting some where as, It was Restarting the Count when you added a New Item in.
 

Attachments

  • Screen shot 2010-04-09 at 21.30.56.png
    Screen shot 2010-04-09 at 21.30.56.png
    294.6 KB · Views: 53
Last edited:
I do not agree with what u are doing now

sorry, if u dun use at least 2 listbox, then it is to use display member and value member of databindings

whatever u are doing now is incorrect. all u need to do is use 2 listboxs 1 for $ and 1 for topic

u can never get the $ field unless u use a substring method of whatever u are doing, which i find it extremely bad
 
Back
Top