Zebra0.com

visual-basic arithmetic

The value of a scroll bar can only be integer. Let's suppose you would like to write a program that lets the user select the cost of a meal and then displays the amount of tip. We would like to let the user select an amount from 0 to $100.00. The solution is to make the Maximum value of the scroll bar 10000. When an amount is selected, the value is divided by 100 to give the desired values.


Start a new program and call it Tip. Build the form as shown in the illustration with the following names and properties:

Labels label:

LblCostHeader with the text "Select the cost of the meal:"
LblCost with the text "$0.00"
LblTipHeader with the text "Leave a tip between"
LblTip1 with the text "$0.00"
LblTipHeader2 with the text "and"
LblTip2 with the text "$0.00"

Horizontal Scroll Bar :

HsbCost with the following properties:

Write the code for the change event on the scroll bar:

Public Class FrmCost
    'Programmer: Janet Joy
    'Calculate amount of tip
    Private Sub HsbCost_Scroll(sender As Object, e As ScrollEventArgs) Handles HsbCost.Scroll
        'Tip is between 15% and 20%
        Dim cost As Double = Me.HsbCost.Value / 100 'divide value by 100 to get 2 decimal places
        Me.LblCost.Text = Format(cost, "$0.00") 'format numbers to 2 decimal places
        Me.LblTip1.Text = Format(cost * 0.15, "$0.00") '15% tip
        Me.LblTip2.Text = Format(cost * 0.2, "$0.00") '20% tip
    End Sub
End Class
Notice that if the value selected on the scroll bar is 900, for example, cost will display as $9.00

NEXT: The mod operator: remainder