הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Sunday, October 13, 2013 6:20 PM
I have a button click event that I need to keep a grand total of items that are added to a listbox of all the information. This code is the first in sequence for this click event. I need for this textbox to keep a running total for the items the user adds to the order with a button click event. I just cannot get the grand total to calculate correctly. I also need for this grand total to reset to 0 when the user clicks the exit button, but keep a running total as long as the user hits the btnAddToOrder. Can you help?
Again, thank you so much for sharing your expertise!
' initialize grand total to 0
decGrandTotal = 0
' calculate grand total
For intCount As Integer = 0 To lstOrderInfo.Items.Count - 1
decGrandTotal = decGrandTotal + decTotalItem
txtDisplayGrandTotal.Text = decGrandTotal.ToString("C")
Next
All replies (3)
Monday, October 14, 2013 1:12 AM ✅Answered
Hi tsphil25,
Where does the variable decTotalItem get it's value from?
It appears that your code adds the value of decTotalItem (which appears to be 0) to decGrandTotal for as many times as there are items in your ListBox lstOrderInfo. Yor result will obviously be 0.
If you wish to add the value of the item in your ListBox you will need code similar to the following:
To test it start a new app and include a TextBox, 2 Buttons and a ListBox. Enter a value into the textBox then click Button 1 to add it to the ListBox, when done click Button2 to get the total of the items in the ListBox.
If this is not what you need then please be more specific.
Regards Ron
Option Strict OnPublic Class Form1 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click ListBox1.Items.Add(TextBox1.Text) End Sub Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click Dim decGrandTotal As Single Dim decTotalItem As Single For i = 0 To ListBox1.Items.Count - 1 If Single.TryParse(CStr(ListBox1.Items.Item(i)), decTotalItem) Then decGrandTotal += decTotalItem End If Next TextBox1.Text = decGrandTotal.ToString("C") End SubEnd Class
Monday, October 14, 2013 12:22 PM ✅Answered
Hello,
The following validates information taken from a textbox is of type decimal then adds it to the ListBox, sets the item as selected then does a total on the numbers. In regards to 0 on exit, that only makes sense if there are no items or all items are 0.
VB.NET VS2010 or higher syntax
Dim decGrandTotal As Decimal = 0
Private Sub Button1_Click(
ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
If Not String.IsNullOrWhiteSpace(TextBox1.Text) Then
If Decimal.TryParse(TextBox1.Text, Nothing) Then
ListBox1.Items.Add(TextBox1.Text)
ListBox1.SelectedIndex = ListBox1.Items.Count - 1
decGrandTotal =
(
From Row In ListBox1.Items.Cast(Of String)()
Select CDec(Row)).ToArray().Sum
Label1.Text = decGrandTotal.ToString("c")
End If
End If
End Sub
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.
Monday, October 14, 2013 12:34 PM