Results 1 to 10 of 10

Thread: VB 2006: accumulators and you

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    4

    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!

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: VB 2006: accumulators and you

    can you post your code + what you've tried?

    Quote Originally Posted by tseegert View Post
    Also, I am working with Visual Studio 2005 in case there are any slight syntax differences.
    there are some huge differences

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    4

    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

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    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

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    4

    Re: VB 2006: accumulators and you

    vb Code:
    1. ' This form calculates calories based on info entered
    2. ' Programmer:  Troy Seegert
    3. ' Date Feb 16
    4.  
    5.  
    6. Public Class Form1
    7.  
    8.     ' Dimension module level variables
    9.     Private accumulatedSumInteger, itemsEnteredInteger As Integer
    10.  
    11.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
    12.         ' this button exits the program
    13.         Me.Close()
    14.  
    15.     End Sub
    16.  
    17.     Private Sub fatGramsLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles fatGramsLabel.Click
    18.         ' this is the fat grams label
    19.     End Sub
    20.  
    21.     Private Sub carbohydratesLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles carbohydratesLabel.Click
    22.         ' this is the carbs label
    23.     End Sub
    24.  
    25.     Private Sub proteinLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles proteinLabel.Click
    26.         ' this is the protein label
    27.     End Sub
    28.  
    29.     Private Sub itemsEnteredLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles itemsEnteredLabel.Click
    30.         ' this is the items entered label
    31.     End Sub
    32.  
    33.     Private Sub totalCaloriesLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles totalCaloriesLabel.Click
    34.         ' this is the total calories label
    35.     End Sub
    36.  
    37.     Private Sub accumulatedSumLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles accumulatedSumLabel.Click
    38.         ' this is the accumulated sum label
    39.     End Sub
    40.  
    41.     Private Sub fatGramsTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles fatGramsTextBox.TextChanged
    42.         ' this is the box to enter fat grams
    43.  
    44.     End Sub
    45.  
    46.     Private Sub carbohydrateGramsTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles carbohydrateGramsTextBox.TextChanged
    47.         ' this is the box to enter carbs grams
    48.     End Sub
    49.  
    50.     Private Sub proteinGramsTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles proteinGramsTextBox.TextChanged
    51.         ' this is the box to enter protein grams
    52.     End Sub
    53.  
    54.     Private Sub itemsEnteredTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles itemsEnteredTextBox.TextChanged
    55.         ' this box shows the total count of items entered
    56.     End Sub
    57.  
    58.     Private Sub totalCaloriesTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles totalCaloriesTextBox.TextChanged
    59.         ' this box shows the total calories for the fat carbs and protein entries
    60.     End Sub
    61.  
    62.     Private Sub accumulatedSumTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles accumulatedSumTextBox.TextChanged
    63.         ' this box shows the accumulated sum of calories until the program is exited
    64.     End Sub
    65.  
    66.     Private Sub calculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculateButton.Click
    67.         ' this is the calculate button
    68.         Dim fatGrams, proteinGrams, totalCalories, carbohydrateGrams As Integer
    69.  
    70.         fatGrams = Integer.Parse(Me.fatGramsTextBox.Text)
    71.         proteinGrams = Integer.Parse(Me.proteinGramsTextBox.Text)
    72.         totalCalories = Integer.Parse(Me.proteinGramsTextBox.Text)
    73.         carbohydrateGrams = Integer.Parse(Me.carbohydrateGramsTextBox.Text)
    74.  
    75.         ' calculate
    76.         Me.accumulatedSumTextBox.Text = fatGrams * 9 + proteinGrams * 4 + carbohydrateGrams * 4 _
    77.         + accumulatedSumInteger
    78.         itemsEnteredInteger = fatGrams + 1
    79.         Me.itemsEnteredTextBox.Text = itemsEnteredInteger.ToString
    80.         Me.totalCaloriesTextBox.Text = fatGrams * 9 + proteinGrams * 4 + carbohydrateGrams * 4 _
    81.         + accumulatedSumInteger
    82.  
    83.     End Sub
    84.  
    85.     Private Sub clearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton.Click
    86.         ' this button clears the fat, carbs, protein, items entered, and total calories values
    87.         Me.fatGramsTextBox.Clear()
    88.         Me.carbohydrateGramsTextBox.Clear()
    89.         Me.proteinGramsTextBox.Clear()
    90.         Me.itemsEnteredTextBox.Clear()
    91.         Me.totalCaloriesTextBox.Clear()
    92.  
    93.     End Sub
    94. End Class

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    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

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: VB 2006: accumulators and you

    is this how you mean it should work?:

    vb Code:
    1. Public Class Form1
    2.  
    3.     ' Dimension module level variables    
    4.     Private accumulatedSumInteger As Integer = 0
    5.     Private itemsEnteredInteger As Integer = 0
    6.  
    7.     Private Sub calculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculateButton.Click
    8.         ' this is the calculate button
    9.         Dim fatGrams, proteinGrams, totalCalories, carbohydrateGrams As Integer
    10.         Integer.TryParse(Me.fatGramsTextBox.Text, fatGrams)
    11.         Integer.TryParse(Me.proteinGramsTextBox.Text, proteinGrams)
    12.         Integer.TryParse(Me.proteinGramsTextBox.Text, totalCalories)
    13.         Integer.TryParse(Me.carbohydrateGramsTextBox.Text, carbohydrateGrams)
    14.         ' calculate
    15.         accumulatedSumInteger += CInt(fatGrams * 9 + proteinGrams * 4 + carbohydrateGrams * 4)
    16.         Me.accumulatedSumTextBox.Text = accumulatedSumInteger.ToString
    17.         itemsEnteredInteger += 1
    18.         Me.itemsEnteredTextBox.Text = itemsEnteredInteger.ToString
    19.         Me.totalCaloriesTextBox.Text = (fatGrams * 9 + proteinGrams * 4 + carbohydrateGrams * 4 _
    20.         + accumulatedSumInteger).ToString
    21.  
    22.     End Sub
    23.  
    24.     Private Sub clearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton.Click
    25.         ' this button clears the fat, carbs, protein, items entered, and total calories values
    26.         Me.fatGramsTextBox.Clear()
    27.         Me.carbohydrateGramsTextBox.Clear()
    28.         Me.proteinGramsTextBox.Clear()
    29.         Me.itemsEnteredTextBox.Clear()
    30.         Me.totalCaloriesTextBox.Clear()
    31.     End Sub
    32.  
    33. End Class

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: VB 2006: accumulators and you

    i'm guessing totalCaloriesTextBox won't be right

  9. #9

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    4

    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!

  10. #10
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    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
  •  



Click Here to Expand Forum to Full Width