Question Calculations in VB

tennisace24

New member
Joined
Mar 27, 2009
Messages
3
Programming Experience
Beginner
I am doing a diet program in VB.net and I have 3 forms which have food and drinks as text boxes which can be selected by the user! I have coded them to be sent to list boxes on another form called 'results'. How can I set it up so what is selected on each form is added to a list box then adds up the calories etc. form what the user selects. I have made a calculate total button to add whats in the list boxes and I need to show the results to the user! How would I go about coding how to do this??? Please help!!!
 
In the main form you'd display the other form like so:
VB.NET:
Using dialogue As new Form2
    If dialogue.ShowDialog() = Windows.Forms.DialogResult.OK Than
        Me.ListBox1.Items.Add(dialogue.SomeProperty)
    End If
End Using
It's up to you to declare the SomeProperty property in your dialogue form and make it return the appropriate data, e.g. the Text from a TextBox. Obviously you's use a more descriptive name than "SomeProperty".
 
The Total button code shows how to add up the values that have been entered in the listbox, and displays the result in a message box. The items in the listbox must be numeric:

Private Sub btnTotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTotal.Click
Dim calorie, total As Integer
For Each x As Integer In ListBox1.Items
calorie = Convert.ToInt32(x)
total = total + calorie
Next
MessageBox.Show("Total calories is: " & total.ToString)
End Sub
 
Last edited:
Just another option, I suggest using a dataset/datatable where you can keep a full list of all foods & drinks along with additional column info such as calories, fat, carbs, protein etc. You can then bind the tables directly for display and selected records dont need to be looped thru to sum up totals.
 
I now have another problem!!! I have done all the program calculations etc. I now add drinks items to a listbox called lstDrinks but I have forms to add items for breakfast and lunch etc. But I also have an add item form and it adds to the listbox fine! But the item that was added from breakfast is for example 'Tea' and then I add an item 'Tea' from the add item form it appears in the listbox under each other! How can I code it to add the amount of teas together and show '2 Teas' in list box n delete the other singular 'Tea'. Any ideas??

P.S. Thanks for the help before!
 
I would use a grid or listview control and display extra columns such as a quantity field for each item
 
Yes but how would I code it cos I dont know how to add up strings together n show three teas instead of three separate teas! What calculation do I do??
 
Back
Top