Page 314 - Open Soource Technologies 304.indd
P. 314

Event Driven Programming


                          sOperator = sNewOperator

                          sRight = “”
                          bLeft = False
                          End If
                          End Sub

                          If bLeft is true, meaning the user has just entered the left part of the calculation, this subroutine
                          sets the sOperator variable we created in step 5 to equal the operator entered, which is passed
                          to AddOperator as the string sNewOperator. The second step is to set bLeft to False, because the
                          entry of an operator means the user is done entering the left side of the equation. In order to
                          handle entries that string multiple operators together, such as 9 * 3 * 2 * 6, we need to also check
                          whether bLeft is false, meaning the user has entered an operator where we were expecting an
                          equals. First we call the Click event for the equals button (described in the next step), which does
                          the calculation and sets tbResult to the result of what has already been entered. Then we clear
                          sRight so the user can enter the next number, and set bLeft to False so the program knows we
                          are entering the right hand side of the calculation next. Finally, add an AddOperator call to the
                          Click event of each operator button, using the same method as we used in step 7 to create the
                          Click events for the number buttons. Code for the plus button will look like this:

                          Private Sub btnPlus_Click()
                          AddOperator (“+”)
                          End Sub
                          Likewise, the code for the minus button will look like this:
                          Private Sub btnMinus_Click()
                          AddOperator (“-”)

                          End Sub
                          9:  Create the Click event for the equals button, which is the most complex code in this program.
                          Create the subroutine structure like we did for the other buttons, by double-clicking the equal’s
                          button on our form. Our subroutine will look like this when we’ve entered the code:
                          Private Sub btnEquals_Click()
                          If sLeft <> “” And sRight = “” And sOperator <> “” Then
                          sRight = sLeft
                          End If

                          If sLeft <> “” And sRight <> “” And sOperator <> “” Then
                          iLeft = sLeft
                          iRight = sRight
                          Select Case sOperator
                          Case “+”

                          iResult = iLeft + iRight
                          Case “-”


                          308                    LOVELY PROFESSIONAL UNIVERSITY
   309   310   311   312   313   314   315   316   317   318   319