Decimal Values with Scroll Bars

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.

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

Labels label:

LblTipHeader 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 HScrollBar HsbCost with the following properties:

Maximum: 10000
LargeChange: 1

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

Private Sub HsbCost_Scroll(ByVal sender As System.Object, ByVal e _
  As System.Windows.Forms.ScrollEventArgs) Handles HsbCost.Scroll
   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 'HsbCost_Scroll
Notice that if the value selected on the scroll bar is 900, for example, cost will display as $9.00
INDEX, The Code View, Selecting code from list, Events, Arguments, Concatenation, Sender, Scroll Bars, Decimal Values on Scroll Bar, Picture Boxes: the Seasons, Comments, Printing, Closing
Next lesson: Functions in Visual Basic

Copyright © Zebra0.com
All rights reserved worldwide.

 
 

Decimal Values on Scroll Bar