Mwagner5600 Posted March 6, 2004 Posted March 6, 2004 (edited) I have a programm where I need to keep a running total of the quantity and the total. I will paste the code I have so far. _________________________________________________ Option Strict On Public Class frmCalorieCounter Inherits System.Windows.Forms.Form " Windows Form Designer generated code " Const mintFAT As Integer = 9 Const mintCARBS_PROTIEN As Integer = 4 Dim mintQuantity As Integer Dim mintTotalCalories As Integer Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click 'Calculates the amount of Calories Try Dim intFat As Integer Dim intCarbs As Integer Dim intProtien As Integer Dim intQuantity As Integer Dim intTotalCalories As Integer Dim intCalories As Integer 'Convert inputValues to numeric variables intFat = CInt(txtFat.Text) intCarbs = CInt(txtCarbs.Text) intProtien = CInt(txtProtien.Text) 'Calculate Values intCalories = intFat * mintFAT + intCarbs * mintCARBS_PROTIEN + intProtien * mintCARBS_PROTIEN intQuantity = intFat + intCarbs + intProtien intTotalCalories = intCalories 'Format and Display answers lblCalories.Text = FormatNumber(intCalories, 0) lblQuantity.Text = FormatNumber(mintQuantity, 0) lblTotalCalories.Text = FormatNumber(mintTotalCalories, 0) Catch MyErr As Exception MsgBox("Try to re-enter your data", MsgBoxStyle.Exclamation, "Data Error") End Try End Sub Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click 'Closes the Programm Me.Close() End Sub Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click 'Clears Proporties lblCalories.Text = "" txtProtien.Clear() txtCarbs.Clear() With txtFat .Clear() .Focus() End With End Sub End Class __________________________________________________________ I will attatch what my form looks like I need to keep a running total of the mintquantity and mintTotalCalories Sorry this is a little basic for all of you but im new to this programm and needed a little help.thanks Mark Edit-Sorry it is the summary part that needs to be the running total that i cant figure outIMAGE.bmp Edited March 6, 2004 by Mwagner5600 Quote
Leaders dynamic_sysop Posted March 7, 2004 Leaders Posted March 7, 2004 if you want to keep a running total you need to use += on your labels when adding the new values , eg: [color=Green]'/// += will cause the count to increase ( adding your new value to the existing one )[/color] lblQuantity.Text += FormatNumber(mintQuantity, 0) lblTotalCalories.Text += FormatNumber(mintTotalCalories, 0) Quote
Mwagner5600 Posted March 7, 2004 Author Posted March 7, 2004 wait it adds it up kinda....but like instead oh adding 3 and 3 together and putting in 6, it puts in 33... Quote
*Experts* mutant Posted March 7, 2004 *Experts* Posted March 7, 2004 Do this: labelname.Text = (Convert.ToInt32(labelname.Text) + numbertoadd).ToString() First you have to convert the text to an integer (or any number data type you want to use) to add it with another number. Since that results in a number, you have to then convert the result to a string. Quote
Mwagner5600 Posted March 7, 2004 Author Posted March 7, 2004 i already converted it to an integer when i formatted it. but what i was saying/typing was I needed to add up the 2 integers in my lable instad of just put 33 add 3+3 and get 6 Quote
*Experts* mutant Posted March 7, 2004 *Experts* Posted March 7, 2004 The code I showed you will do it the way you want... Quote
Mwagner5600 Posted March 7, 2004 Author Posted March 7, 2004 hmm well its not exactly working for me. when im debugging the programm and type in 1 for fat 1 for carbs and 1 for protien it says 17 calores and 3 items (quantity) but then if i clear, and do it again it will say the same thing except 1717 calories and 33 items. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.