Zebra0.com

csharp 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:

lblInstructions with the text "Select the cost of the meal:"
lblCost with the text "$0.00"
lblTip with the text "Leave a tip between"
lblTip1 with the text "$0.00"
lblAnd 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:

private void hsbCost_Scroll(object sender, ScrollEventArgs e)
{
   // Tip is between 15% and 20%
   double cost = this.hsbCost.Value / 100; // divide value by 100 to get 2 decimal places
   lblCost.Text = cost.ToString("$0.00"); // format numbers to 2 decimal places
   double tip1 = cost * 0.15; // 15% tip
   lblTip1.Text = tip1.ToString("$0.00"); // format with 2 decimal places
   double tip2 = cost * 0.2; // 20% tip
   lblTip2.Text = tip2.ToString("$0.00"); // format with 2 decimal places
}
Notice that if the value selected on the scroll bar is 900, for example, cost will display as $9.00

End of lesson, Next lesson: