Xtreme Visual Basic Talk

Xtreme Visual Basic Talk (http://www.xtremevbtalk.com/)
-   .NET Installation / Documentation (http://www.xtremevbtalk.com/-net-installation-documentation/)
-   -   food menu (http://www.xtremevbtalk.com/-net-installation-documentation/326529-food-menu.html)

Pyromark 10-22-2013 02:41 PM

food menu
Im making a food menu program for class. i have 3 buttons for the sizes of pizza. When i click small. label on the right comes up with 1 and then puts 6.00 in subtotal box. when i click medium, it doesn't add to the 6.00 it just puts its own price as 9.00. how do i add them all together.
Public Class Form1

Private Sub btnSmall_Click(sender As Object, e As EventArgs) Handles btnSmall.Click lblSmall.Text = Val(lblSmall.Text) + 1 Dim declblSubtotal As Integer declblSubtotal = Val(lblSmall.Text) * 6 lblSubtotal.Text = declblSubtotal.ToString("c") End Sub Private Sub btnMedium_Click(sender As Object, e As EventArgs) Handles btnMedium.Click lblMedium.Text = Val(lblMedium.Text) + 1 Dim declblSubtotal As Integer declblSubtotal = Val(lblMedium.Text) * 9 lblSubtotal.Text = declblSubtotal.ToString("c") End Sub

Icehawke 10-22-2013 04:08 PM

You are resetting the value of declblSubtotal each time you click the buttons. You need to widen the scope of that variable.

Pyromark 10-22-2013 04:23 PM

how so? :)

Gruff 10-22-2013 04:55 PM

If you read up on scope you will see that among other things it controls
where variables can be seen and affected by code.

Variables created inside subroutines are created and destroyed all within the sub routine.

Dim A as integer = 6 '(Variable defined before any sub routines are global to the form) and exist until the form is closed. Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click A += 12 'A now equals 18 'A += 12 is equivalent to saying A = A + 12 End Sub Private Sub btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click A += 22 'A now equals 40 End Sub

Pyromark 10-22-2013 04:59 PM

thanks for the reply but does that make it so everytime i click the button it keeps adding whatever the price is?

Icehawke 10-23-2013 07:49 AM

If you click btn1 once, A is 18. If you click it a second time, A becomes 30. A third time and 42. And so on.

Gruff 10-23-2013 09:13 AM


The value of A will be increased each time you click a button.
The total will be which buttons you pressed and how many times.

The idea here is that you use this knowledge to fix your original code where A is your declblSubtotal variable.

Pyromark 10-23-2013 10:13 AM

ohhhh i understand now. but do i have to fix it so EACH code is like that? bc i have 3 buttons. i have it down to were when i click small 3 times, yes it will add up but when i click medium, it will automatically start over at 9$ for medium when i want it to actually add to the existing price

Pyromark 10-23-2013 10:14 AM

i understand what ur saying but can you get me started on how to actually change it?

AtmaWeapon 10-23-2013 10:31 AM

Think about what you are doing mathematically.

The subtotal should be:
subtotal = ax + by + cz

Right now your code either uses:
subtotal = ax
subtotal = by

How could you change it so every time you have the correct subtotal?

Pyromark 10-23-2013 10:46 AM

I need it so subtotal = ax + bx +cx

Pyromark 10-23-2013 10:47 AM

ax + by + cz * u know what i mean i have it so each button it puting the correct price in the subtotal box, but idk where to put or how to put the code to add them all up

Gruff 10-23-2013 11:24 AM

You were given more than enough information in this thread to solve your problem.
It is not our job to write your code for you.

Remove: Dim declblSubtotal As Integer (From each button_click code event)
Add: one copy of it before all of your button_click events. (See Post #4 defining A)

Change: declblSubtotal = Val(<Label>.Text) * n
declblSubtotal += Val(<Label>.Text) * n (In each button_click event)

Notice the += operator in the corrected code. += adds a value to the existing variable value

All times are GMT -6. The time now is 10:38 AM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2018, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO v2.0.15 (Lite) - vBulletin Mods & Addons Copyright © 2018 DragonByte Technologies Ltd.
All site content is protected by the Digital Millenium Act of 1998. Copyright©2001-2011 MAS Media Inc. and Extreme Visual Basic Forum. All rights reserved.
You may not copy or reproduce any portion of this site without written consent.