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 jmcilhinney; 06-03-2012 at 1:20 AM.
Reason: Added [xcode=vb] tags and significantly improved spacing for readability
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
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
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.
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
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
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 jmcilhinney; 06-07-2012 at 1:10 AM.
Reason: Added [xcode=vb] tags for readability
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.
Dim x As Short
If Not Short.TryParse(TextBox1.Text.Trim, x) Then
'Parse failed, should tell the user somehow
Return
End If
'Now use x
Originally Posted by Dunfiddlin
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
intTotal = intTotal + 4
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
intTotal += 4
End Sub
Originally Posted by sanctity
This is everything; I truly appreciate all of the help.
Essentially this is more of what you're probably trying to do here, this is a project I put together to illustrate what we're talking about. It increments 2 totals with each addition & includes a way to remove an item from the listbox and decrement those totals:
Option Explicit On
Option Strict On
Option Infer Off
Public Class MainForm
'Class level variables to hold the totals
Private m_TotalItems As Integer = 0I
Private m_TotalCost As Decimal = 0D
Private Sub AddButton_Click(sender As System.Object, e As System.EventArgs) Handles AddButton.Click
If Item1NumericUpDown.Value > 0D Then
'If there are any quantities of "Item 1" create a new ItemClass for it, add the class to the ListBox then increment the totals
'The item's price that's convertible to a decimal is in the PriceLabel's Tag property
Dim newItem As New ItemClass("Item 1", CInt(Item1NumericUpDown.Value), CDec(Item1PriceLabel.Tag))
ItemsListBox.Items.Add(newItem)
m_TotalItems += newItem.Quantity
m_TotalCost += newItem.GetCost()
Item1NumericUpDown.Value = 0D
End If
If Item2NumericUpDown.Value > 0D Then
'If there are any quantities of "Item 2" create a new ItemClass for it, add the class to the ListBox then increment the totals
'The item's price that's convertible to a decimal is in the PriceLabel's Tag property
Dim newItem As New ItemClass("Item 2", CInt(Item2NumericUpDown.Value), CDec(Item2PriceLabel.Tag))
ItemsListBox.Items.Add(newItem)
m_TotalItems += newItem.Quantity
m_TotalCost += newItem.GetCost()
Item2NumericUpDown.Value = 0D
End If
If Item3NumericUpDown.Value > 0D Then
'If there are any quantities of "Item 3" create a new ItemClass for it, add the class to the ListBox then increment the totals
'The item's price that's convertible to a decimal is in the PriceLabel's Tag property
Dim newItem As New ItemClass("Item 3", CInt(Item3NumericUpDown.Value), CDec(Item3PriceLabel.Tag))
ItemsListBox.Items.Add(newItem)
m_TotalItems += newItem.Quantity
m_TotalCost += newItem.GetCost()
Item3NumericUpDown.Value = 0D
End If
If Item4NumericUpDown.Value > 0D Then
'If there are any quantities of "Item 4" create a new ItemClass for it, add the class to the ListBox then increment the totals
'The item's price that's convertible to a decimal is in the PriceLabel's Tag property
Dim newItem As New ItemClass("Item 4", CInt(Item4NumericUpDown.Value), CDec(Item4PriceLabel.Tag))
ItemsListBox.Items.Add(newItem)
m_TotalItems += newItem.Quantity
m_TotalCost += newItem.GetCost()
Item4NumericUpDown.Value = 0D
End If
'Update the label's on the form
TotalItemsLabel.Text = m_TotalItems.ToString
TotalCostLabel.Text = m_TotalCost.ToString("c")
End Sub
Private Sub RemoveToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles RemoveToolStripMenuItem.Click
'Get the Listbox's selected item, cast it to type ItemClass
Dim selItem As ItemClass = CType(ItemsListBox.SelectedItem, ItemClass)
'Decrement the total's
m_TotalItems -= selItem.Quantity
m_TotalCost -= selItem.GetCost()
'Remove the item from the ListBox
ItemsListBox.Items.Remove(selItem)
'Update the total's on the form
TotalItemsLabel.Text = m_TotalItems.ToString
TotalCostLabel.Text = m_TotalCost.ToString("c")
End Sub
Private Sub ItemsListBox_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ItemsListBox.MouseDown
ItemsListBox.SelectedIndex = ItemsListBox.IndexFromPoint(e.Location)
End Sub
Private Sub ItemsListBox_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ItemsListBox.SelectedIndexChanged
RemoveToolStripMenuItem.Enabled = ItemsListBox.SelectedIndex > -1I
End Sub
End Class
'This class is used in the ListBox
Friend Class ItemClass
Friend Sub New(Itm As String, Qty As Integer, Prc As Decimal)
Me.ItemName = Itm
Me.Quantity = Qty
Me.Price = Prc
End Sub
Friend Property ItemName As String = String.Empty
Friend Property Quantity As Integer = 0I
Friend Property Price As Decimal = 0D
Friend Function GetCost() As Decimal
Return Me.Quantity * Me.Price
End Function
'This is how we control what text is displayed in the ListBox, the LB call's the item's ToString() method and this is what gets returned from that call
Public Overrides Function ToString() As String
Return String.Format("{0}: {1} @ {2} = {3}", Me.ItemName, Me.Quantity.ToString("000"), Me.Price.ToString("c"), Me.GetCost.ToString("c"))
End Function
End Class
Yes. You do have to take care that the value of decRunningTotal is not altered at any other point in the program. And lest the program is closed (accidentally or otherwise) it would make sense to write the value to a file every time it's updated as a back up.
Bookmarks