sas

Topic: Menu-Driven and Calculator Projects


     In this lesson we will consider two projects, one a menu driven drawing project and the other a calculator project.   Menus are one of the most important and most characteristic elements of the Windows User Interface.   Menu Commands are similar to Controls.   They recognize only the click events.   When you group controls as arrays, give them the same name property, but give their index property as 0, 1, 2, 3 etc. to distinguish them.

  1. Menu Driven Project:

A view of the Menu bar and some Menu Items:

     Suppose we want to build a program with a menu bar with two titles and one or more menu items for each of the menu titles somewhat as shown in the figure 1.   In this example the menu items denote different pixel values for drawing lines.   The different menu items are connected to appropriate codes which will enable you to select the pixel thickness from the menu and draw figures of of different thickness.

m1

                                  Figure 1

Menu Editor Window:

     Start VB from the Standard EXE mode.   Select the item “Menu Editor” from the Tools Menu.   The Menu Editor Window appears as shown in the figure 2.   This window lets you create a menu bar that contains all the menu items you want.

m2

                                    Figure 2

Creating the Menu Bar:

     In this project we will have only two menu titles in the menu bar.   Of course one can have as many as required for the concerned problem.   The first title is ‘Draw’.   Type ‘&Draw’ in the Caption field.   The & symbol before D tells that D is a hot key and users can use alt+D key combination to pull down the menu.   Next press the Tab key.   This moves the insertion point to the next text box ‘Name’, which is the name of the control.   Enter ‘menDraw’ in the Name field and click the right arrow key at the bottom of the menu editor window.   This is how the computer recognizes the item as menu title.  The prefix ‘men’ is used to indicate to the users that the item ‘Draw’ is the name of the menu title.   The Caption field is cleared to enable the next menu title to be entered.   Now enter ‘&Quit’ in the Caption field and ‘menQuit’ in the Name field and click OK.   Now the design window will show a menu bar with two items ‘Draw’ and ‘Quit’.   At the bottom you will see the two items entered as shown in the figure 2a.

&Draw
&Quit

Figure 2a

Creating Menu Items:

     Now we have to enter the menu items under the menu titles Draw and Quit. B  Open the Menu Editor Window.   Select &Quit.  Select Insert.   This action will insert a line between the two items.   Type &Erase in the Caption field and miErase in the Name field.   The prefix ‘mi’ indicates that the item ‘Erase’ is a  menu item and not a menu title.  Now the bottom window will appear as shown in the figure 2b.

 &Draw
 …&Erase
 &Quit

  Figure 2b

In a similar manner add the menu item Exit for the menu title Quit.   Use the Caption name as E&xit and the control name as miExit.

Adding Control Arrays:

     Next we have to add the three line-width menu items.  The first one will have the Caption &1 Pixel and a control name ‘miPixel’   When different controls have the same array name they must be identified by indexes.   So after entering the control name, move the insertion point to the index field and enter 1 for the first  Pixel Control.   For the subsequent Pixel Controls enter the index values as 2 and 3 respectively.

Inserting Lines in Menus

     After highlighting  the item next to the place where you want to insert a line, select insert button.   Type a hyphen(-) symbol for the Caption name and miLine1 for the Control name.   For subsequent lines give the control names as muLine2, miLine3 etc.   Finally the design window will appear as shown in the figure 3.   Insertion or deletion of lines and menu titles and menu items can be done with the help of insert, delete and OK buttons.

Entering Codes for the Controls and Pop-up Menu:

     When you select any control, the code window for the particular control will be opened.   Enter the codes for the different controls as shown in the figure 3a.   A pop-up menu is the same as the other menus, except that it does not drop down from the menu bar, but displayed on the form at the place where you right-click the mouse.   The code for creating the pop-up menu is also shown in the figure 3a.
                                
Private numPoints As Integer     ' Number of points saved
Private saveX(1000) As Single    ' Saved X coordinates
Private saveY(1000) As Single    ' Saved Y coordinates
Private numLines As Integer        ' Number of separate lines
Private lineStart(500) As Integer  ' Start of each line
Private lineEnd(500) As Integer    ' End of each line
Private lineThickness(500) As Integer  ' DrawWidth for
                                       ' each line
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  PSet (X, Y)                  ' Draw first point in line
  numPoints = numPoints + 1    ' The next free point
  saveX(numPoints) = X         ' Remember this point
  saveY(numPoints) = Y
  numLines = numLines + 1         ' Next free line
  lineStart(numLines) = numPoints ' Index of the first point
  lineEnd(numLines) = numPoints   ' Initially a single point
 End Sub
lineThickness(numLines) = DrawWidth
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  If Button = 1 Then           ' Is the left button down?
    Line -(X, Y)               ' Yes, draw a line
    numPoints = numPoints + 1   ' Next free point in array
    saveX(numPoints) = X       ' Remember this point
    saveY(numPoints) = Y
   lineEnd(numLines) = numPoints  ' New ending index for line
  End If
End Sub

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  If Button = 2 Then          'Right mouse button clicked?
    PopupMenu menDraw         'Yes. Display menDraw as popup.
    End If
End Sub

Private Sub Form_Paint()
  For Lin = 1 To numLines      ' Draw each line
    aStart = lineStart(Lin)    ' Start index of this line
    anEnd = lineEnd(Lin)       ' End index of this line
    DrawWidth = lineThickness(Lin)
    PSet (saveX(aStart), saveY(aStart))
    For i = aStart To anEnd     ' Draw parts of this line
      Line -(saveX(i), saveY(i))
    Next i
  Next Lin
End Sub

Private Sub miErase_Click()
    Cls
  numPoints = 0          ' Set to no points
  numLines = 0           ' Set to no lines
End Sub

Private Sub miexit_Click()
End
End Sub

Private Sub miPixel_Click(Index As Integer)
  miPixel(DrawWidth).Checked = False  ' Uncheck current width
  DrawWidth = Index                   ' Change width of the pen
  miPixel(Index).Checked = True       ' Check the new width
End Sub

Private Sub miPrint_Click()
  For Lin = 1 To numLines      ' Draw each line
    aStart = lineStart(Lin)    ' Start index of this line
    anEnd = lineEnd(Lin)       ' End index of this line
    Printer.DrawWidth = lineThickness(Lin)
    Printer.PSet (saveX(aStart), saveY(aStart))
    For i = aStart To anEnd     ' Draw parts of this line
      Printer.Line -(saveX(i), saveY(i))
    Next i
  Next Lin
  Printer.EndDoc
End Sub
     Figure 3a

Running the Project:

     Select Start from the Run Menu.   Then by clicking the menu items draw the word hello for three different thickness.   The final run mode window will look as shown in the figure 4.

 m3

                            Figure 3

m4

                           Figure 4

2. Calculator Project:

     The basic layout of the screen is shown in the figure 5.   There are two control arrays of buttons one for the digits 0 to 9 and another for operators +, -, /, * and =.   The form is given the name ‘calculator’ and its icon property is set to calculator icon.   The display label is given the name ‘Readout’ and the caption ‘0.’. The number buttons 0 to 9  are given the same name ‘Number’, their captions and indexes are set as 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9 respectively.    The operator buttons /, +, X, - and = are given the same name, “Operator” , and their indexes are set as 0, 1, 2, 3 and 4 respy.   The ‘%’  button is given the name ‘Percent’, the ‘.’  button is given the name ‘Decimal’, the ‘C’ button is given the name ‘Cancel’ and the ‘CE’ button is given the name ‘CancelEntry’.    The codes are entered in the code window  as shown in the figure 5a.   Save and run the project and  check that the calculator functions properly.  

Option Explicit
Dim Op1, Op2                ' Previously input operand.
Dim DecimalFlag As Integer  ' Decimal point present yet?
Dim NumOps As Integer       ' Number of operands.
Dim LastInput               ' Indicate type of last keypress event.
Dim OpFlag                  ' Indicate pending operation.
Dim TempReadout
' Click event procedure for C (cancel) key.
' Reset the display and initializes variables.
Private Sub Cancel_Click()
    Readout = Format(0, "0.")
    Op1 = 0
    Op2 = 0
    Form_Load
End Sub
' Click event procedure for CE (cancel entry) key.
Private Sub CancelEntry_Click()
    Readout = Format(0, "0.")
    DecimalFlag = False
    LastInput = "CE"
End Sub
' Click event procedure for decimal point (.) key.
' If last keypress was an operator, initialize
' readout to "0." Otherwise, append a decimal
' point to the display.
Private Sub Decimal_Click()
    If LastInput = "NEG" Then
        Readout = Format(0, "-0.")
    ElseIf LastInput <> "NUMS" Then
        Readout = Format(0, "0.")
    End If
    DecimalFlag = True
    LastInput = "NUMS"
End Sub

' Initialization routine for the form.
' Set all variables to initial values.
Private Sub Form_Load()
    DecimalFlag = False
    NumOps = 0
    LastInput = "NONE"
    OpFlag = " "
    Readout = Format(0, "0.")
    'Decimal.Caption = Format(0, ".")
End Sub
' Click event procedure for number keys (0-9).
' Append new number to the number in the display.
Private Sub Number_Click(Index As Integer)
    If LastInput <> "NUMS" Then
        Readout = Format(0, ".")
        DecimalFlag = False
    End If
    If DecimalFlag Then
        Readout = Readout + Number(Index).Caption
    Else
        Readout = Left(Readout, InStr(Readout, Format(0, ".")) - 1) + Number(Index).Caption + Format(0, ".")
    End If
    If LastInput = "NEG" Then Readout = "-" & Readout
    LastInput = "NUMS"
End Sub
' Click event procedure for operator keys (+, -, x, /, =).
' If the immediately preceeding keypress was part of a
' number, increments NumOps. If one operand is present,
' set Op1. If two are present, set Op1 equal to the
' result of the operation on Op1 and the current
' input string, and display the result.

Private Sub Operator_Click(Index As Integer)
    TempReadout = Readout
    If LastInput = "NUMS" Then
        NumOps = NumOps + 1
    End If
    Select Case NumOps
        Case 0
        If Operator(Index).Caption = "-" And LastInput <> "NEG" Then
            Readout = "-" & Readout
            LastInput = "NEG"
        End If
        Case 1
        Op1 = Readout
        If Operator(Index).Caption = "-" And LastInput <> "NUMS" And OpFlag <> "=" Then
            Readout = "-"
            LastInput = "NEG"
        End If
        Case 2
        Op2 = TempReadout
        Select Case OpFlag
            Case "+"
                Op1 = CDbl(Op1) + CDbl(Op2)
            Case "-"
                Op1 = CDbl(Op1) - CDbl(Op2)
            Case "X"
                Op1 = CDbl(Op1) * CDbl(Op2)
            Case "/"
                If Op2 = 0 Then
                   MsgBox "Can't divide by zero", 48, "Calculator"
                Else
                   Op1 = CDbl(Op1) / CDbl(Op2)
                End If
            Case "="
                Op1 = CDbl(Op2)
            Case "%"
                Op1 = CDbl(Op1) * CDbl(Op2)
            End Select
        Readout = Op1
        NumOps = 1
    End Select
    If LastInput <> "NEG" Then
        LastInput = "OPS"
        OpFlag = Operator(Index).Caption
    End If
End Sub
' Click event procedure for percent key (%).
' Compute and display a percentage of the first operand.
Private Sub Percent_Click()
    Readout = Readout / 100
    LastInput = "Ops"
    OpFlag = "%"
    NumOps = NumOps + 1
    DecimalFlag = True
End Sub

Figure 5a

m5

                            Figure 5

Prev

 

 

 

 

 

 

 

 

 


footer back link


 

Error in my_thread_global_end(): 1 threads didn't exit