'---------------------------------------------------------- ' Lab 2 (A201) ' CoinCounter ' ' This program will ask the user to enter a number coins into ' appropriate textboxes, and then will calculate the total ' dollars and cents. ' Prior to calculating the totals, the program will also perform ' some error checking to make sure the values in the textboxes ' represent integer numbers. Option Strict On Option Explicit On '------------------------------------------------ Public Class Form1 '------------------------------------------------ Private Sub BtnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnQuit.Click Application.Exit() ' same as saying Me.Close() End Sub '------------------------------------------------ Private Sub BtnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClear.Click ' Clear out the textboxes TxtBxQuarters.Text = "" TxtBxDimes.Text = "" TxtBxNickels.Text = "" TxtBxPennies.Text = "" TxtBxDollars.Text = "" TxtBxCents.Text = "" End Sub '------------------------------------------------ Private Sub BtnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCalculate.Click Dim Total As Integer = 0 ' Declare an integer variable VerifyTextboxes() ' ERROR Checking: Verify the content of textboxes Total = CInt(TxtBxQuarters.Text) * 25 + CInt(TxtBxDimes.Text) * 10 + CInt(TxtBxNickels.Text) * 5 + CInt(TxtBxPennies.Text) * 1 TxtBxDollars.Text = CStr(Total \ 100) ' Integer division, and implicit type conversion TxtBxCents.Text = CStr(Total Mod 100) ' Remainder, and implicit type conversion End Sub '------------------------------------------------ ' Check the content of each textbox, make sure the ' content is numeric, if it not warn the user by ' changing the color of the textbox to red, then ' display a message explaining what the problem is, ' and finally replace the content with a zero (0) and ' change the color of the textbox back to white. Private Sub VerifyTextboxes() If IsNumeric(TxtBxQuarters.Text) Then TxtBxQuarters.Text = CStr(Int(TxtBxQuarters.Text)) ' convert to integer and truncate decimals Else TxtBxQuarters.BackColor = System.Drawing.Color.Red MessageBox.Show("Number of quarters must be expressed as an integer") TxtBxQuarters.Text = "0" TxtBxQuarters.BackColor = System.Drawing.Color.White End If If IsNumeric(TxtBxDimes.Text) Then TxtBxDimes.Text = CStr(Int(TxtBxDimes.Text)) Else TxtBxDimes.BackColor = System.Drawing.Color.Red MessageBox.Show("Number of dimes must be expressed as an integer") TxtBxDimes.Text = "0" TxtBxDimes.BackColor = System.Drawing.Color.White End If If IsNumeric(TxtBxNickels.Text) Then TxtBxNickels.Text = CStr(Int(TxtBxNickels.Text)) Else TxtBxNickels.BackColor = System.Drawing.Color.Red MessageBox.Show("Number of nickels must be expressed as an integer") TxtBxNickels.Text = "0" TxtBxNickels.BackColor = System.Drawing.Color.White End If If IsNumeric(TxtBxPennies.Text) Then TxtBxPennies.Text = CStr(Int(TxtBxPennies.Text)) Else TxtBxPennies.BackColor = System.Drawing.Color.Red MessageBox.Show("Number of pennies must be expressed as an integer") TxtBxPennies.Text = "0" TxtBxPennies.BackColor = System.Drawing.Color.White End If End Sub End Class