Page 315 - Open Soource Technologies 304.indd
P. 315
Unit 14: Building Small Application
iResult = iLeft – iRight
Case “/”
iResult = iLeft / iRight
Case “*”
iResult = iLeft * iRight
End Select
tbResult.Text = iResult
sLeft = iResult
sRight = “”
bLeft = True
End If
End Sub
The first three lines of code check to see if both sides of the calculation have been entered along
with an operator. If only the left side and an operator are entered, the value of the left side is
copied to the right, so we can mimic the standard calculator behavior for handling an entry like
9 * =, which multiplies 9 by itself to get a result of 81. The rest of the code will run only if left,
right, and operator are entered, and starts out by copying the strings of numbers into our iLeft
and iRight Double-typed variables, which can do the actual calculations. The Select Case statement
allows us to run different code depending on which operator was entered, and performs the
actual calculation, placing the result in iResult. Finally, we update the textbox with the result,
copy the result into sLeft, reset sRight, and set bLeft = True. These last lines allow us to take the
result of the calculation and use it to perform another calculation.
10: Handle the last three operation buttons: sqrt, %, and 1/x. For the Click event of the square
root button, Code will look like this:
Private Sub btnSqrt_Click()
If sLeft <> “” Then
iLeft = sLeft
Else
iLeft = 0
End If
If sRight <> “” Then
iRight = sRight
Else
iRight = 0
End If
If bLeft Then
iLeft = Math.Sqr(iLeft)
LOVELY PROFESSIONAL UNIVERSITY 309