Question List Box Issue

sanctity

Member
Joined
Jun 2, 2012
Messages
7
Programming Experience
1-3
Private Sub btnEOD_Click
    ' My issue here is that when the items in my list box are being added it doesn't them together (Decx = decx + decSubTotal) 
    ' It is multiplying the last entry by the amount of entries entered. (All entries are ranndom and vary in quantity. I am trying to make an end of day sales total.)

    decItemCount = CDec(lstTotalSales.Items.Count)

    Do Until decItemCount = 0
        decItemCount -= 1
        decx = decx + decSubTotal
    Loop

    strX = decx.ToString("C")
    lblX.Text = strX

    ' This part adds the second numerical variable in the list box

    decItemCount2 = CDec(lstTotalSales.Items.Count())

    Do Until decItemCount2 = 0
        decItemCount2 -= 1
        decY = decY + decTotal
    Loop

    strY = decY.ToString("C")
    lblY.Text = strY
End Sub
 
Last edited by a moderator:
What is it that you're actually trying to achieve? That's always a good bit of information to provide. My guess is that your ListBox contains Decimal values and you want to sum them. Is that correct? If so then the first thing you would need to do is actually get the values from the ListBox. Nowhere in that code do you actually get an item from the ListBox so you're never going to be able to do anything with those items.

You should be using a For Each loop for that, not a Do loop. For each item in the ListBox, add that item to a running total. The running total will obviously need to be initialised to zero.

If I have guessed wrongly then that is a perfect example of why you need to provide a FULL and CLEAR description of what you're actually trying to achieve.
 
What is it that you're actually trying to achieve? That's always a good bit of information to provide. My guess is that your ListBox contains Decimal values and you want to sum them. Is that correct? If so then the first thing you would need to do is actually get the values from the ListBox. Nowhere in that code do you actually get an item from the ListBox so you're never going to be able to do anything with those items.

You should be using a For Each loop for that, not a Do loop. For each item in the ListBox, add that item to a running total. The running total will obviously need to be initialised to zero.

If I have guessed wrongly then that is a perfect example of why you need to provide a FULL and CLEAR description of what you're actually trying to achieve.

Sorry I didnt explain it well enough. I am trying to add all decimals entered into the listbox, that are random and the amount of entries are also random. I never used a For Each Loop, but I tried and this is where I got stuck:

Dim newColl As New Collection From {lstTotalSales}
For Each decSubTotal As String In newColl
If IsNumeric(decSubTotal) Then
decx +=decx +
End If
Next



I need to find decX I believe.
 
sanctity, where's the data in the listbox coming from? is it user entered or from a data source?

If you're storing each transaction as they are entered into a datasource, like a database, perhaps querying it with the sum already added up would be a better approach.
If you have to get it from the listbox then something along these lines is what you're needing:
Dim RunningTotal As Decimal = 0
Dim CurrAmount As Decimal
For Each Item As Object in lstItems
    'I haven't seen any code showing how items are added to the listbox, so this kind of safety net code may not be needed
    If Not Decimal.TryParse(If Item.ToString.Trim.Length = 0, "0", Item.ToString.Trim), CurrAmount) Then CurrAmount = 0
    RunningTotal += CurrAmount
Next
 
sanctity, where's the data in the listbox coming from? is it user entered or from a data source?

If you're storing each transaction as they are entered into a datasource, like a database, perhaps querying it with the sum already added up would be a better approach.
If you have to get it from the listbox then something along these lines is what you're needing:
Dim RunningTotal As Decimal = 0
Dim CurrAmount As Decimal
For Each Item As Object in lstItems
    'I haven't seen any code showing how items are added to the listbox, so this kind of safety net code may not be needed
    If Not Decimal.TryParse(If Item.ToString.Trim.Length = 0, "0", Item.ToString.Trim), CurrAmount) Then CurrAmount = 0
    RunningTotal += CurrAmount
Next

Actually JB, the If statement isn't required because, if TryParse returns False, the out parameter is set to zero anyway.
 
sanctity, where's the data in the listbox coming from? is it user entered or from a data source?

If you're storing each transaction as they are entered into a datasource, like a database, perhaps querying it with the sum already added up would be a better approach.
If you have to get it from the listbox then something along these lines is what you're needing:
Dim RunningTotal As Decimal = 0
Dim CurrAmount As Decimal
For Each Item As Object in lstItems
    'I haven't seen any code showing how items are added to the listbox, so this kind of safety net code may not be needed
    If Not Decimal.TryParse(If Item.ToString.Trim.Length = 0, "0", Item.ToString.Trim), CurrAmount) Then CurrAmount = 0
    RunningTotal += CurrAmount
Next

All of my data is user entered. Data is entered into multiple textboxes then a button sums the total price, that data is stored in the listbox as:
lstTotalSales.Items.Add ("Items Sold:" & intAmountOfItems & " " & decSubTotal & " " & decTotal)
Now what I need to do is add the decSubTotal and decTotal
Then add the 2 final values into a lbl below the list box.
Everything thing else I understand. I just can't figure out how to add items stored in the listbox.. I tried the code format you gave me, all the was output was 0.00.
I know I'm not the best at this.
 
I tried the code format you gave me, all the was output was 0.00.
I know I'm not the best at this.

That would be because the values in your ListBox are not actually Decimals and therefore cannot be parsed as such. This is why it is CRITICAL that you provide a FULL and CLEAR explanation of the whole problem in the first place. If you make us guess then there's every chance that we'll guess wrong and that's a waste of everyone's time. You said that you were adding Decimals to the ListBox but you're not. You're adding Strings. Those Strings are partly constructed from Decimals but that doesn't change the fact that they are Strings.

Why don't you simply add the values to some variables as you add the items to the list? That would be the easiest way. Just declare a couple of member variables, initialise them to zero and, each time you add an item to the ListBox, add the appropriate Decimal values to those numbers. There's no need for any loop at the end then because you've been adding the numbers as you go.
 
All of my data is user entered. Data is entered into multiple textboxes then a button sums the total price, that data is stored in the listbox as:
lstTotalSales.Items.Add ("Items Sold:" & intAmountOfItems & " " & decSubTotal & " " & decTotal)
Now what I need to do is add the decSubTotal and decTotal
Then add the 2 final values into a lbl below the list box.
Everything thing else I understand. I just can't figure out how to add items stored in the listbox.. I tried the code format you gave me, all the was output was 0.00.
I know I'm not the best at this.
So what you're saying is, you build a string with all of the info and now you're wanting to parse that just to get a daily total?

I would recommend making a class to store that info and you add a new instance of the class to the ListBox. The class will override the ToString() method and can output the string in the same format:
Friend Class SoldItem

    Friend Sub New(NumberItems As Integer, STotal As Decimal, DTotal As Decimal)
        Me.NumberOfItems = NumberItems
        Me.SubTotal = STotal
        Me.Total = DTotal
    End Sub

    Friend Property NumberOfItems As Integer
    Friend Property SubTotal As Decimal
    Friend Property Total As Decimal

    Public Overrides Function ToString() As String
        Return String.Format("Items Sold:{0} {1} {2}", Me.NumberOfItems, Me.SubTotal.ToString("c"), Me.Total.ToString("c"))
    End Function

End Class
Then to add an item to it:
lstTotalSales.Items.Add(New SoldItem(intAmountOfItems, decSubTotal, decTotal))
And lastly to get those running totals back:
        Dim RunningSubTotal As Decimal = 0
        Dim RunningTotal As Decimal = 0

        For Each Item As SoldItem In lstTotalSales.Items
            RunningSubTotal += Item.SubTotal
            RunningTotal += Item.Total
        Next
And there you have it, welcome to Object Oriented Programming (OOP)

Also, as JMC has suggested, incrementing the totals each time an item is added to the ListBox would save time at the end because the daily total is already accumulated rather than looping the listbox to get the same number.
 
I don't know why, but nothing worked. I tried it, I most likely didn't do it right but in any case I tried to do something else to get the same result. Now, I still cant get it just right.

here is my code:


Dim decFinalValue As Decimal = 0


If Me.Flag1 = 1 Then
decFinalValue += decFinalValue + decSubTotal


Flag1 = 0


End If
string2 = decFinalValue.ToString("C")
lblX.Text = string2

Flag1 is for every time a calculate decSubTotal button click event perform this operation. But, it doesn't store the information after I click the calculate decSubTotal button again. I've grown sick of this problem I just want to get it done already.
 
What's the full code you have for this?

It looks like you're going for the increment the total value each time an item is added to the listbox. if so, the only thing you need to do is add the decFinalValue += decSubTotal line right after the Listbox.items.add() line and of course update the label on the form.
 
This is everything; I truly appreciate all of the help.

Option Strict On
Public Class frmPOS
    'Dims everything
    Dim decItemCount As Decimal
    Dim decItemCount2 As Decimal
    Dim decY As Decimal
    Dim strY As String
    Dim Flag1 As Integer


    ' Constant values




    Dim decKidShirt As Decimal = 3
    Dim decManShirt As Decimal = 1
    Dim decWomanShirt As Decimal = 1


    Dim decKidLegwear As Decimal = 1
    Dim decManLegwear As Decimal = 1
    Dim decWomanLegwear As Decimal = 1


    Dim decKidUnder As Decimal = 1
    Dim decManUnder As Decimal = 1
    Dim decWomenUnder As Decimal = 1


    Dim decKidWinter As Decimal = 1
    Dim decManWinter As Decimal = 1
    Dim decWomanWinter As Decimal = 1


    Dim decKidShoes As Decimal = 1
    Dim decManShoes As Decimal = 1
    Dim decWomanShoes As Decimal = 1


    Dim decSubTotal As Decimal


    Dim decTotal As Decimal
    Dim strTotal As String


    Dim strchange As String
    Dim decchange As Decimal




    'Strings
    Dim strChildrenTopwear As String
    Dim strMenTopwear As String
    Dim strWomenTopwear As String


    Dim strChildrenBottomwear As String
    Dim strMenBottomwear As String
    Dim strWomenBottomwear As String


    Dim strChildrenUndergarments As String
    Dim strMenUndergarments As String
    Dim strWomenUndergarment As String


    Dim strChildrenWinterwear As String
    Dim strMenWinterwear As String
    Dim strWomenWinterwear As String


    Dim strChildrenShoes As String
    Dim strMenShoes As String
    Dim strWomenShoes As String


    Dim strCustomPrice As String
    Dim strCustomAmount As String
    ' Interger from strings
    Dim decx As Decimal
    Dim strX As String


    Dim intChildrenTopwear As Integer
    Dim intMenTopwear As Integer
    Dim intWomenTopwear As Integer


    Dim intChildrenBottomwear As Integer
    Dim intMenBottomwear As Integer
    Dim intWomenBottomwear As Integer


    Dim intChildrenUndergarments As Integer
    Dim intMenUndergarments As Integer
    Dim intWomenUndergarments As Integer


    Dim intChildrenWinterwear As Integer
    Dim intMenWinterwear As Integer
    Dim intWomenWinterwear As Integer


    Dim intChildrenShoes As Integer
    Dim intMenShoes As Integer
    Dim intWomenShoes As Integer


    Dim decCustomPrice As Decimal
    Dim intCustomQuantity As Integer


    Dim strTax As String
    Dim decTax As Decimal


    Dim decChangetext As Decimal


    Dim strChangetext As String
    Dim intAmountOf As Integer
    Dim strListBoxText As String = "Items Sold X"
    Dim strTest As String




    Private Sub btnCalculateSubTotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateSubTotal.Click
        'Converts textbox to string to integer
        'Also the Flag+=1 code for the bottom
        Flag1 += 1
        strChildrenTopwear = txtChildrenTopwear.Text
        strMenTopwear = txtMenTopwear.Text
        strWomenTopwear = txtWomenTopwear.Text


        strChildrenBottomwear = txtChildrenBottomwear.Text
        strMenBottomwear = txtMenBottomwear.Text
        strWomenBottomwear = txtWomenBottomwear.Text


        strChildrenUndergarments = txtChildrenUndergarments.Text
        strMenUndergarments = txtMenUndergarments.Text
        strWomenUndergarment = txtWomenUndergarments.Text


        strChildrenWinterwear = txtChildrenWinterwear.Text
        strMenWinterwear = txtMenWinterWear.Text
        strWomenWinterwear = txtWomenWinterwear.Text


        strChildrenShoes = txtChildrenShoes.Text
        strMenShoes = txtMenShoes.Text
        strWomenShoes = txtWomenShoes.Text


        strCustomAmount = txtCustomQuantity.Text
        strCustomPrice = txtCustomItemPrice.Text


        intChildrenTopwear = Convert.ToInt32(strChildrenTopwear)
        intMenTopwear = Convert.ToInt32(strMenTopwear)
        intWomenTopwear = Convert.ToInt32(strWomenTopwear)


        intChildrenBottomwear = Convert.ToInt32(strChildrenBottomwear)
        intMenBottomwear = Convert.ToInt32(strMenBottomwear)
        intWomenBottomwear = Convert.ToInt32(strWomenBottomwear)


        intChildrenUndergarments = Convert.ToInt32(strChildrenUndergarments)
        intMenUndergarments = Convert.ToInt32(strMenUndergarments)
        intWomenUndergarments = Convert.ToInt32(strWomenUndergarment)


        intChildrenWinterwear = Convert.ToInt32(strChildrenWinterwear)
        intMenWinterwear = Convert.ToInt32(strMenWinterwear)
        intWomenWinterwear = Convert.ToInt32(strWomenWinterwear)


        intChildrenShoes = Convert.ToInt32(strChildrenShoes)
        intMenShoes = Convert.ToInt32(strMenShoes)
        intWomenShoes = Convert.ToInt32(strWomenShoes)


        intCustomQuantity = Convert.ToInt32(strCustomAmount)
        decCustomPrice = Convert.ToDecimal(strCustomPrice)


        'Calculates amount of items
        intAmountOf = (intChildrenBottomwear + intChildrenShoes + intChildrenTopwear + intChildrenUndergarments + intChildrenWinterwear + intMenBottomwear + intMenShoes + intMenTopwear + intMenUndergarments + intMenWinterwear + intWomenBottomwear + intWomenShoes + intWomenTopwear + intWomenUndergarments + intWomenWinterwear + intCustomQuantity)
        'Calculates amount of price before tax
        decSubTotal = (CDec(((intChildrenBottomwear * decKidLegwear) + (intChildrenShoes * decKidShoes) + (intChildrenTopwear * decKidShirt) + (intChildrenUndergarments * decKidUnder) + (intChildrenWinterwear * decKidWinter) + (intMenBottomwear * decManLegwear) + (intMenShoes * decManShoes) + (intMenTopwear * decManShirt) + (intMenUndergarments * decManUnder) + (intMenWinterwear * decManWinter) + (intWomenBottomwear * decWomanLegwear) + (intWomenShoes * decWomanShoes) + (intWomenTopwear * decWomanShirt) + (intWomenUndergarments * decWomenUnder) + (intWomenWinterwear * decWomanWinter) + (intCustomQuantity * (decCustomPrice)))))
        strTest = decSubTotal.ToString("C")
        'Puts numbers into text boxes associated
        lblSubTotalCost.Text = strTest


        decTax = CDec((decSubTotal * 0.07))
        lblTax.Text = decTax.ToString("C")


        decTotal = decSubTotal + decTax
        lblTotalCost.Text = decTotal.ToString("C")
        'list box add items code
        lstTotalSales.Items.Add(strListBoxText & intAmountOf & "    " & decSubTotal & "    " & decTotal)
    End Sub
    Friend Sub btnCalculateChange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateChange.Click
        'Simply calculates change from entere value from total.
        strchange = txtReceived.Text
        If txtReceived.Text = "" Then
            MsgBox("Please enter a number.", CType(32, MsgBoxStyle), "Error")




        Else : IsNumeric(strchange)
            decchange = Convert.ToDecimal(strchange)
            decChangetext = decchange - decTotal
            strChangetext = decChangetext.ToString("C")
            lblChange.Text = strChangetext
        End If


    End Sub




    Private Sub btnEOD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEOD.Click
        'My entire problem lies here. I need to add all subtotal values to a single value. Honestly the items show up in the list box don't need to be added
        ' they can be added however it works I dont care anymore. The first block here enables and disables the button to add up everything.
        Dim intClickTimes As Integer
        Dim string2 As String
        intClickTimes += 1


        If intClickTimes = 1 Then
            btnEOD.Enabled = False
        End If




        Dim decFinalValue As Decimal = 0


        If Me.Flag1 = 1 Then
            decFinalValue += decFinalValue + decSubTotal


            Flag1 = 0


        End If
        string2 = decFinalValue.ToString("C")
        lblX.Text = string2






     
    End Sub


    Private Sub frmPOS_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
    End Sub


    Private Sub ClearAllToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearAllToolStripMenuItem.Click
        txtChildrenBottomwear.Text = "0"
        txtChildrenShoes.Text = "0"
        txtChildrenTopwear.Text = "0"
        txtChildrenUndergarments.Text = "0"
        txtChildrenWinterwear.Text = "0"
        txtCustomItemPrice.Text = "0"
        txtCustomQuantity.Text = "0"
        txtMenBottomwear.Text = "0"
        txtMenShoes.Text = "0"
        txtMenTopwear.Text = "0"
        txtMenUndergarments.Text = "0"
        txtMenWinterWear.Text = "0"
        txtReceived.Text = "0"
        txtWomenBottomwear.Text = "0"
        txtWomenShoes.Text = "0"
        txtWomenTopwear.Text = "0"
        txtWomenUndergarments.Text = "0"
        txtWomenWinterwear.Text = "0"
        lblSubTotalCost.Text = ""
        lblTotalCost.Text = ""
        lblTax.Text = ""
        lblChange.Text = ""




    End Sub


    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txtChildrenBottomwear.Text = "0"
        txtChildrenShoes.Text = "0"
        txtChildrenTopwear.Text = "0"
        txtChildrenUndergarments.Text = "0"
        txtChildrenWinterwear.Text = "0"
        txtCustomItemPrice.Text = "0"
        txtCustomQuantity.Text = "0"
        txtMenBottomwear.Text = "0"
        txtMenShoes.Text = "0"
        txtMenTopwear.Text = "0"
        txtMenUndergarments.Text = "0"
        txtMenWinterWear.Text = "0"
        txtReceived.Text = "0"
        txtWomenBottomwear.Text = "0"
        txtWomenShoes.Text = "0"
        txtWomenTopwear.Text = "0"
        txtWomenUndergarments.Text = "0"
        txtWomenWinterwear.Text = "0"
        lblSubTotalCost.Text = ""
        lblTotalCost.Text = ""
        lblTax.Text = ""
        lblChange.Text = ""


    End Sub


    Private Sub btnAble_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAble.Click
        If btnEOD.Enabled = False Then
            btnEOD.Enabled = True
        ElseIf btnEOD.Enabled = True Then
            btnEOD.Enabled = False
        End If


    End Sub


    Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
        Close()


    End Sub
End Class
 
Last edited by a moderator:
Don't know whether this has been resolved or not but my first thought is that you've more or less programmed a spreadsheet application from scratch thereby at least tripling the necessary work. Why did you not use a spreadsheet which would do all the calculations more or less automatically and where necessary supplement it with a programmed interface (although from what I see here I'm not sure that there is a where necessary!) There's an awful lot of redundant code here. Why, for example, would you dim a separate string variable for every text box entry for subsequent conversion to integers when the textbox text is already a string? And what's with all those decimal constants of 1?​
 
Don't know whether this has been resolved or not but my first thought is that you've more or less programmed a spreadsheet application from scratch thereby at least tripling the necessary work. Why did you not use a spreadsheet which would do all the calculations more or less automatically and where necessary supplement it with a programmed interface (although from what I see here I'm not sure that there is a where necessary!) There's an awful lot of redundant code here. Why, for example, would you dim a separate string variable for every text box entry for subsequent conversion to integers when the textbox text is already a string? And what's with all those decimal constants of 1?​

You can't convert txtBox.text to a numerical value. You can convert strings to numbers. The decimal constants are prices I'm using to test the program. Also, I have 2 clear buttons. To be honest I've given up on this project. Nobody seems to be able to help me. I'm guessing it's impossible to add values over multiple button clicks.
 
Er , yes you can! Textbox.Text is just another string variable. This works .... try it!

x = Convert.ToInt16(TextBox1.Text)

And there's no difficulty at all in totalling over multiple button clicks using a global variable.

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
intTotal = intTotal + 4
End Sub

... just keeps adding 4 every time you click the button as long as intTotal is not altered in any other part of the program.

You've just over-complicated this. I really don't understand why you need the flag variable for example. All you have to do is add the sub-total to a running total global variable every time the sub total is calculated. btnEOD shouldn't have anything to do with totalling, if it's needed at all. Everything can be done in the btnCalculateSubTotal click routine ...

calculate total items price
add tax
display transaction total
add transaction total to running day total

Similarly you can keep running totals of any other categories, eg. items sold without ever straying outside the subtotal routine.
 
Er , yes you can! Textbox.Text is just another string variable. This works .... try it!

x = Convert.ToInt16(TextBox1.Text)

And there's no difficulty at all in totalling over multiple button clicks using a global variable.

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
intTotal = intTotal + 4
End Sub

... just keeps adding 4 every time you click the button as long as intTotal is not altered in any other part of the program.

You've just over-complicated this. I really don't understand why you need the flag variable for example. All you have to do is add the sub-total to a running total global variable every time the sub total is calculated. btnEOD shouldn't have anything to do with totalling, if it's needed at all. Everything can be done in the btnCalculateSubTotal click routine ...

calculate total items price
add tax
display transaction total
add transaction total to running day total

Similarly you can keep running totals of any other categories, eg. items sold without ever straying outside the subtotal routine.

Well, at any rate, you are saying that in the calculate total price button I can add code like:

decRunningTotal +=decSubtotal
Would that update everytime I click the button? Because the value of the subtotal changes every purchase of course.
 
Back
Top