Start fresh Start VS 2005 and Create a new project: Calculator_IT1C_xx Choose appropriate directory and remember its path Exit VS 2005 Unzip start_cal.zip: Form1.vb and Form1.Designer.vb Use File explorer to replace the two newly created project files in Calculator_IT1C_xx
ButtonSqrt_Click Double click on the button for Sqrt to generate the ButtonSqrt_Click subroutine Copy the codes from ButtonEqual_Click Modify the following line to use Math.Sqrt total = total + x total = Math.Sqrt(x)
ButtonPercent_Click Double click on the button for % to generate the ButtonPercent_Click subroutine Copy the codes from ButtonEqual_Click Modify the following line to use Math.Sqrt total = total + x total = x / 100
Dot Private Sub ButtonDot_Click ‘ Copy the codes from Button0 ‘ Replace “0” with “.” Dim existingString As String = TextBox1.Text If existingString <> "" Then TextBox1.Text = existingString & "0" End If End Sub “.”
Testing 1.5 OK 0.5 NG => How?
Dot Private Sub ButtonDot_Click ‘ Add else condition ‘ to add “0.” Dim existingString As String = TextBox1.Text If existingString <> "" Then TextBox1.Text = existingString & “.” End If End Sub Else TextBox1.Text = existingString & “0.” End If End Sub
Testing 1.5 OK 0.5 OK 1.5.5. => should not allow – How?
How to stop “1.5.5.”? We need to use a global variable to track if “.” has been set Then add additional codes in ButtonDot to check this variable
Dot Global variable: Public Class Form1 Dim isDotted As Boolean = False
Dot Private Sub ButtonDot_Click ‘ Add Condition to check for isDotted Dim existingString As String = TextBox1.Text If existingString <> "" Then TextBox1.Text = existingString & “.” Else TextBox1.Text = existingString & “0.” End If End Sub If isDotted = False Then isDotted= True ‘ Set isDotted to True End If End Sub
Testing 1.5 OK 0.5 OK 1.5.5. OK 1.5 Clear 1.5 => NG - How?
Dot Private Sub ButtonClear_Click : : ‘ Reset isDotted to False isDotted = False End Sub
Testing 1.5 OK 0.5 OK 1.5.5. OK 1.5 Clear 1.5 OK 1.5 + 1.5 => NG – How?
Dot Private Sub ButtonPlus_Click : : ‘ Reset isDotted to False isDotted = False End Sub
Testing 1.5 OK 0.5 OK 1.5.5. OK 1.5 Clear 1.5 OK 1.5 + 1.5 OK Anymore cases?
Minus, Multiply and Divide Before we proceed to add the processing for Minus, Multiply and Divide, we need to modify the current processing for Plus and Equal
Modify Plus and Equal Private Sub ButtonPlus_Click : : total = total + x : End Sub total = x ‘ Now, Plus can only be ‘ pressed once.
Modify Plus and Equal Global variable: Public Class Form1 Dim whatFunction As String = “None”
Modify Plus and Equal Private Sub ButtonPlus_Click : : whatFunction = “Plus” End Sub
Modify Plus and Equal Private Sub ButtonEqual_Click : : total = total + x : End Sub Select Case whatFunction Case “Plus” total = total + x End Select Case “Minus”
Add Minus, Multiply and Divide Complete the processing for minus, multiply and divide. Take care of divide by zero error with if then else statement Do testingand debugging to take care of other errors
0 comments
Post a comment