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

Event Driven Programming



                          Select the CommandButton icon on the toolbar and create the first button the same way we
                          created the TextBox to add buttons. For reference, use the Windows calculator in Standard view
                          (Programs > Accessories > Calculator) as a basis for calculator layout, leaving out the “MC”,
                          “MR”, “MS”, and “M+” buttons. On each button, change the following properties (using the “+”
                          button as an example): “(Name)” = btnPlus, “Caption” = +. Do the same for the rest of the
                          calculator buttons, and then save work. This form should now resemble the example shown here.
                          5: Add the code. Note that if buttons and textbox are not named the same as the code listed here
                          expects, we will need to change the names to match buttons and textbox, or change buttons and
                          textbox to match this code. First we need to create a few variables for processing calculator input:
                          Dim sLeft As String, sRight As String, sOperator As String
                          Dim iLeft As Double, iRight As Double, iResult As Double
                          Dim bLeft As Boolean
                          Each calculation consists of four parts: a number to the left of the operator (sLeft, iLeft), an
                          operator (sOperator), a number to the right of the operator (sRight, iRight), and a result (iResult).
                          In order to track whether the user is entering the left or right number, we need to create a
                          boolean variable, bLeft. If bLeft is true, the left side of the calculation is being entered; if bLeft
                          is false, the right side is being entered.
                          6:  Initialize the bLeft variable. We do that by creating a Form_Load subroutine, which we can
                          either type as listed here or automatically create by double-clicking on any part of the form not
                          covered by a button or textbox. Inside the function, we need to set bLeft to True, because the
                          first number entered will be the left part.
                          Private Sub Form_Load()
                          bLeft = True
                          End Sub
                          7:




























                                                 Fig. 14.10:  Code listing after step 7 (ALLD)




                          306                    LOVELY PROFESSIONAL UNIVERSITY
   307   308   309   310   311   312   313   314   315   316   317