Question Adding together variables and displaying in a label?

dimitarberbatov

New member
Joined
Nov 11, 2008
Messages
2
Programming Experience
Beginner
Hi I'm building a cash register and need some. I need help with adding my TotalCost together, I need to add ExtBurger, ExtFries and ExtDrink together to get my total, but not sure which event will need to trigger.

here is a sample of my code:
VB.NET:
Private Sub VScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles VScrollBar1.Scroll
    
 lblQtyBurger.Text = VScrollBar1.Value

        Dim BurgerPrice As Decimal
        Dim ExtBurger As Decimal

        BurgerPrice = 1.44

        ExtBurger = VScrollBar1.Value * BurgerPrice
        

        lblBurgerPrice.Text = BurgerPrice
        lblExtBurger.Text = ExtBurger
VB.NET:
Private Sub VScrollBar2_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles VScrollBar2.Scroll
        lblQtyFries.Text = VScrollBar2.Value

        Dim FriesPrice As Decimal
        Dim ExtFries As Decimal
        

        FriesPrice = 0.74
        ExtFries = VScrollBar2.Value * FriesPrice
        


        lblFriesPrice.Text = FriesPrice
        lblExtFries.Text = ExtFries


    End Sub
VB.NET:
Private Sub VScrollBar3_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles VScrollBar3.Scroll
        lblQtyDrink.Text = VScrollBar3.Value

        Dim DrinkPrice As Decimal
        'Dim ExtDrink As Decimal

        DrinkPrice = 0.66
        ExtDrink = VScrollBar3.Value * DrinkPrice
        

        lblDrinkPrice.Text = DrinkPrice
        lblExtDrink.Text = ExtDrink

    End Sub
I need to add ExtBurger, ExtFries and ExtDrink together to get a total and display it in lblTotal

If anyone could point me in the right direction on which event will need to trigger the calulation, where to declare the variables etc?

any help would be brilliant
thanks
 
Last edited by a moderator:
I would create a sub that simply adds the totals and displays them in the appropriate label(s) then simply call that sub at the end of the three subs you've posted.
 
First of all, turn Option Strict On and place that statement at the top of your code. Obviously, you haven't done that since you didn't bother explicitly converting your text to numbers.

After making the explicit conversions from string to number variables, you can perform math calculation using the number variables. Then convert the number result to a string and output the string result to a label.

In summary: All inputs must be done with strings. If arithmetic computation is needed, the strings must be converted into numbers. After math processing is completed, the result must be converted back into a string for output.
 
I'm wondering what his UI looks like, I'm willing to bet he could remove those scrollbars and some of the labels, replace them with a couple of NumericUpDown's
 
Thanks for the replys I'm quite new to vb only started learning a couple of weeks ago, thanks for both sets of answers!

I created a sub

Private Sub CalculateTotal()

TotalCost = ExtBurger + ExtFries + ExtDrink

lblTotalCost.Text = TotalCost

and it seems to have done the trick, now I just need to figure out how to work out the sales tax and delivery zone costs with radiobuttons!
 
Back
Top