Page 316 - Open Soource Technologies 304.indd
P. 316
Event Driven Programming
tbResult.Text = iLeft
Else
iRight = Math.Sqr(iLeft)
tbResult.Text = iRight
End If
If iLeft <> 0 Then
sLeft = iLeft
Else
sLeft = “”
End If
If iRight <> 0 Then
sRight = iRight
Else
sRight = “”
End If
End Sub
The first 11 lines of code make sure that if we don’t have a value entered for either side of the
equation, we substitute zero instead of trying to copy an empty string into iLeft or iRight, which
will generate an error. The middle lines perform the square root function on the current part
of the calculation, either left or right. Finally, we reverse the checks we did in the beginning so
that a zero is copied as an empty string back into sLeft and sRight. For the percent button, the
code is similar, with one exception: the percent operation can only be performed if both left and
right sides are entered.
Private Sub btnPercent_Click()
If Not bLeft Then
If sRight <> “” Then
iRight = sRight
Else
iRight = 0
End If
iRight = iRight * (iLeft / 100)
tbResult.Text = iRight
If iRight <> 0 Then
sRight = iRight
Else
sRight = “”
310 LOVELY PROFESSIONAL UNIVERSITY