-
Feb 27th, 2012, 02:47 PM
#1
Thread Starter
New Member
VB 2006: accumulators and you
Hi all, my name is Troy. I am new to VB, but slowly grasping the concepts (some come much easier than others). I am trying to create a program that counts calories. I have all but finished this assignment, but I am having trouble with one aspect of it. I have found information on how to count and accumulate items, but only from a single source. My problem is that I need to accumulate a quantity of 1 into an "items entered" box for each click of the calculate button, and there are up to three items that can be entered at a time. That being said, I cannot simply add 3 to the previous total, as not every box will have information entered every time.
Any suggestions would be greatly appreciated. Been trying to figure this one out on and off for a week. Also, I am working with Visual Studio 2005 in case there are any slight syntax differences.
Well met!
-
Feb 27th, 2012, 02:50 PM
#2
Re: VB 2006: accumulators and you
can you post your code + what you've tried?
Originally Posted by tseegert
Also, I am working with Visual Studio 2005 in case there are any slight syntax differences.
there are some huge differences
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 27th, 2012, 02:55 PM
#3
Thread Starter
New Member
Re: VB 2006: accumulators and you
I will post the code, but I havent really tried as every source of information I have found does not give an example on how to accumulate from multiple sources. But, have a looksee!
' This form calculates calories based on info entered
' Programmer: Troy Seegert
' Date Feb 16
Public Class Form1
' Dimension module level variables
Private accumulatedSumInteger, itemsEnteredInteger As Integer
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
' this button exits the program
Me.Close()
End Sub
Private Sub fatGramsLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles fatGramsLabel.Click
' this is the fat grams label
End Sub
Private Sub carbohydratesLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles carbohydratesLabel.Click
' this is the carbs label
End Sub
Private Sub proteinLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles proteinLabel.Click
' this is the protein label
End Sub
Private Sub itemsEnteredLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles itemsEnteredLabel.Click
' this is the items entered label
End Sub
Private Sub totalCaloriesLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles totalCaloriesLabel.Click
' this is the total calories label
End Sub
Private Sub accumulatedSumLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles accumulatedSumLabel.Click
' this is the accumulated sum label
End Sub
Private Sub fatGramsTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles fatGramsTextBox.TextChanged
' this is the box to enter fat grams
End Sub
Private Sub carbohydrateGramsTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles carbohydrateGramsTextBox.TextChanged
' this is the box to enter carbs grams
End Sub
Private Sub proteinGramsTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles proteinGramsTextBox.TextChanged
' this is the box to enter protein grams
End Sub
Private Sub itemsEnteredTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles itemsEnteredTextBox.TextChanged
' this box shows the total count of items entered
End Sub
Private Sub totalCaloriesTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles totalCaloriesTextBox.TextChanged
' this box shows the total calories for the fat carbs and protein entries
End Sub
Private Sub accumulatedSumTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles accumulatedSumTextBox.TextChanged
' this box shows the accumulated sum of calories until the program is exited
End Sub
Private Sub calculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculateButton.Click
' this is the calculate button
Dim fatGrams, proteinGrams, totalCalories, carbohydrateGrams As Integer
fatGrams = Integer.Parse(Me.fatGramsTextBox.Text)
proteinGrams = Integer.Parse(Me.proteinGramsTextBox.Text)
totalCalories = Integer.Parse(Me.proteinGramsTextBox.Text)
carbohydrateGrams = Integer.Parse(Me.carbohydrateGramsTextBox.Text)
' calculate
Me.accumulatedSumTextBox.Text = fatGrams * 9 + proteinGrams * 4 + carbohydrateGrams * 4 _
+ accumulatedSumInteger
itemsEnteredInteger = fatGrams + 1
Me.itemsEnteredTextBox.Text = This is where I am stuck
Me.totalCaloriesTextBox.Text = fatGrams * 9 + proteinGrams * 4 + carbohydrateGrams * 4 _
+ accumulatedSumInteger
End Sub
Private Sub clearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton.Click
' this button clears the fat, carbs, protein, items entered, and total calories values
Me.fatGramsTextBox.Clear()
Me.carbohydrateGramsTextBox.Clear()
Me.proteinGramsTextBox.Clear()
Me.itemsEnteredTextBox.Clear()
Me.totalCaloriesTextBox.Clear()
End Sub
End Class
-
Feb 27th, 2012, 03:02 PM
#4
Re: VB 2006: accumulators and you
a looksee??? can you format your code? above the Message editor window is a Code button + a VBCode button... when posting you highlight the code + click either button.
to edit existing code, click the Edit button at the bottom right of the post, click the Go Advanced button in the editor window, then highlight the code + click either button
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 27th, 2012, 03:07 PM
#5
Thread Starter
New Member
Re: VB 2006: accumulators and you
vb Code:
' This form calculates calories based on info entered ' Programmer: Troy Seegert ' Date Feb 16 Public Class Form1 ' Dimension module level variables Private accumulatedSumInteger, itemsEnteredInteger As Integer Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click ' this button exits the program Me.Close() End Sub Private Sub fatGramsLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles fatGramsLabel.Click ' this is the fat grams label End Sub Private Sub carbohydratesLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles carbohydratesLabel.Click ' this is the carbs label End Sub Private Sub proteinLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles proteinLabel.Click ' this is the protein label End Sub Private Sub itemsEnteredLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles itemsEnteredLabel.Click ' this is the items entered label End Sub Private Sub totalCaloriesLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles totalCaloriesLabel.Click ' this is the total calories label End Sub Private Sub accumulatedSumLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles accumulatedSumLabel.Click ' this is the accumulated sum label End Sub Private Sub fatGramsTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles fatGramsTextBox.TextChanged ' this is the box to enter fat grams End Sub Private Sub carbohydrateGramsTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles carbohydrateGramsTextBox.TextChanged ' this is the box to enter carbs grams End Sub Private Sub proteinGramsTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles proteinGramsTextBox.TextChanged ' this is the box to enter protein grams End Sub Private Sub itemsEnteredTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles itemsEnteredTextBox.TextChanged ' this box shows the total count of items entered End Sub Private Sub totalCaloriesTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles totalCaloriesTextBox.TextChanged ' this box shows the total calories for the fat carbs and protein entries End Sub Private Sub accumulatedSumTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles accumulatedSumTextBox.TextChanged ' this box shows the accumulated sum of calories until the program is exited End Sub Private Sub calculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculateButton.Click ' this is the calculate button Dim fatGrams, proteinGrams, totalCalories, carbohydrateGrams As Integer fatGrams = Integer.Parse(Me.fatGramsTextBox.Text) proteinGrams = Integer.Parse(Me.proteinGramsTextBox.Text) totalCalories = Integer.Parse(Me.proteinGramsTextBox.Text) carbohydrateGrams = Integer.Parse(Me.carbohydrateGramsTextBox.Text) ' calculate Me.accumulatedSumTextBox.Text = fatGrams * 9 + proteinGrams * 4 + carbohydrateGrams * 4 _ + accumulatedSumInteger itemsEnteredInteger = fatGrams + 1 Me.itemsEnteredTextBox.Text = itemsEnteredInteger.ToString Me.totalCaloriesTextBox.Text = fatGrams * 9 + proteinGrams * 4 + carbohydrateGrams * 4 _ + accumulatedSumInteger End Sub Private Sub clearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton.Click ' this button clears the fat, carbs, protein, items entered, and total calories values Me.fatGramsTextBox.Clear() Me.carbohydrateGramsTextBox.Clear() Me.proteinGramsTextBox.Clear() Me.itemsEnteredTextBox.Clear() Me.totalCaloriesTextBox.Clear() End Sub End Class
-
Feb 27th, 2012, 03:41 PM
#6
Re: VB 2006: accumulators and you
Looksee is a term I've heard before, but it may be regional.
You are going to want to use TryParse rather than Parse on those textboxes. Search the forum for thousands of examples of it. Parse will still throw an exception that you don't want.
You don't really want textboxes to display information. Textboxes tell the user that they can edit the value, which is not the case for several of your boxes, such as TotalCalories, and so forth. For those things that are just informational, and which the user shouldn't edit, use a label.
Since this is an assignment, that poses some problems. After all, what appears to me to be the best solution may be something that you haven't covered, yet. I would suggest that you make a class for each item, with fields for protein, carbs, and fat. Then you could have a List (of thatItem). However, if you haven't worked with classes, that won't be such a good plan. One alternative might be to have a List for protein, a List for Fat, and a List for Carbs, or you could just have a list of Integer holding the total calories in each item entered.
That's about all I can say, though, because I'm so hungry that just thinking about either of those three will risk shorting out my keyboard from drool.
My usual boring signature: Nothing
-
Feb 27th, 2012, 03:43 PM
#7
Re: VB 2006: accumulators and you
is this how you mean it should work?:
vb Code:
Public Class Form1
' Dimension module level variables
Private accumulatedSumInteger As Integer = 0
Private itemsEnteredInteger As Integer = 0
Private Sub calculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculateButton.Click
' this is the calculate button
Dim fatGrams, proteinGrams, totalCalories, carbohydrateGrams As Integer
Integer.TryParse(Me.fatGramsTextBox.Text, fatGrams)
Integer.TryParse(Me.proteinGramsTextBox.Text, proteinGrams)
Integer.TryParse(Me.proteinGramsTextBox.Text, totalCalories)
Integer.TryParse(Me.carbohydrateGramsTextBox.Text, carbohydrateGrams)
' calculate
accumulatedSumInteger += CInt(fatGrams * 9 + proteinGrams * 4 + carbohydrateGrams * 4)
Me.accumulatedSumTextBox.Text = accumulatedSumInteger.ToString
itemsEnteredInteger += 1
Me.itemsEnteredTextBox.Text = itemsEnteredInteger.ToString
Me.totalCaloriesTextBox.Text = (fatGrams * 9 + proteinGrams * 4 + carbohydrateGrams * 4 _
+ accumulatedSumInteger).ToString
End Sub
Private Sub clearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton.Click
' this button clears the fat, carbs, protein, items entered, and total calories values
Me.fatGramsTextBox.Clear()
Me.carbohydrateGramsTextBox.Clear()
Me.proteinGramsTextBox.Clear()
Me.itemsEnteredTextBox.Clear()
Me.totalCaloriesTextBox.Clear()
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 27th, 2012, 03:45 PM
#8
Re: VB 2006: accumulators and you
i'm guessing totalCaloriesTextBox won't be right
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 27th, 2012, 04:09 PM
#9
Thread Starter
New Member
Re: VB 2006: accumulators and you
Yes Shaggy, I was just re-reading and noticed Tryparse would be what I wanted to use to avoid exceptions when nothing is entered in a field. As far as the textboxes, I agree and thought it odd but believe it or not, that is what the instructions for this particular assignment stated. Thanks for your suggestions, I will certainly check them all out. I apologize for my "looksee"... I got that from my mother years ago and sometimes it just happens
Paul, I am studying your revision, thanks very much for taking the time. I will re-read the chapter as I obviously missed a couple things; it can get quite confusing learning this type of material in a distance-learning format so believe me when I say that I much appreciate any and all help.
Again, thanks and I will use your advice and dig in!
-
Feb 27th, 2012, 05:38 PM
#10
Re: VB 2006: accumulators and you
Setting the ReadOnly property on a TextBox to true is a technique I've used for displaying values to the user. It makes it clear that it isn't a field the user can edit, but stands out from the Label controls so the information doesn't get overlooked as a static caption.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|