' Mr. Minich
' Period 1
' Ch4Demo2
' 11/2/2000
' Purpose - to illustrate the use of the KeyPress event and a Select Case statement

Option Explicit

Private Sub Form_Activate()
    ' displaying appropriate messages & setting visibility
    
    lblMessage.AutoSize = True  ' allows message to fit into labels without being
    lblWarning.AutoSize = True  '   cut off
    
                                ' setting various captions
    lblMessage.Caption = "Press a to add 1" & vbCrLf & "Press s to subtract 1" _
        & vbCrLf & "Press r for a random value"
    lblRunningTotal.Caption = "0"
    lblWarning.Caption = "Press 'a' to add one and 's' to subtract one"
    cmdExit.Caption = "E&xit"
    cmdClear.Caption = "&Clear"
    
    lblWarning.Visible = False  ' warning should be invisible at first
    
    frmMain.KeyPreview = True   ' necessary to for KeyPress to work with
                                '   command buttons on the form
    
End Sub

Private Sub Form_KeyPress(KeyAscii As Integer)
    ' adds or subtracts one depending on the user's keypress
    
    '   KeyAscii is the parameter used to store the ASCII value of the key
    '   that was pressed by the user.

    lblWarning.Visible = False  ' make warning invisible in case it was visible
    
    Select Case KeyAscii
    Case 97                     ' if a is pressed add one
        lblRunningTotal.Caption = Val(lblRunningTotal.Caption) + 1
    Case 115                    ' if s is pressed subtract one
        lblRunningTotal.Caption = Val(lblRunningTotal.Caption) - 1
    Case 113, 119, 69, 68, 99, 120, 122 ' if any key around a or s is pressed end immediately
        MsgBox "You shouldn't have done that! Game Over!"
        cmdExit_Click           ' calling cmdExit_Click to end the program
    Case 114                    ' set to a random integer between 0 and 99
                                '   if r is pressed
        lblRunningTotal.Caption = Int(Rnd * 100)
    Case Else                   ' show warning if any other key pressed
        lblWarning.Visible = True
    End Select
    
End Sub

Private Sub cmdClear_Click()
    ' clear running total label
    
    lblRunningTotal.Caption = "0"
End Sub

Private Sub cmdExit_Click()
    ' unload form and exit project
    
    Unload Me
    End
End Sub

' Notes

' Notice the use of the comma in the third Case of the Select Case. 

' Any event procedure can be called from within another event procedure simply by placing its
'	name on a line of code. The cmdExit_Click event procedure is called from within the third
'	case of the Select Case statement in this way. Of course the "Unload Me" and "End" statements
'	could have been placed there instead of the call statement but this demonstrates the 
'	reusability of code. The statement

' cmdExit_Click
' is a call statement. ' The Rnd function is used in the fourth case of the Select Case statement to generate a random ' number. Visual Basic will return a random decimal number between (but not including) 0 and 1 when Rnd is used. ' I multiplied that value by 100 so that it is a decimal number between (but not including) 0 and 100. ' Then, I used the Int function to turn that decimal number into a whole number between or including 0 and 99.
' As it is used in this program, the Rnd function always returns the same sequence of random values as the user ' presses the r key each time the program runs. ' The MsgBox statement is used within the Select Case statement to cause a message box to display on the screen. ' A message box requires the user to read the message it displays and to then press the OK button in order to ' continue with the program's execution. Message boxes are useful when interacting with the user but they should ' not be overused. ' The Select Case statement could be replaced by the following If/ElseIf statement. However, it is considered to be ' bad style to use an If/ElseIf if it would be easier to read an equivalent Select Case statement. ' If (KeyAscii = 97) Then ' lblRunningTotal.Caption = Val(lblRunningTotal.Caption) + 1 ' ElseIf (KeyAscii = 115) Then ' lblRunningTotal.Caption = Val(lblRunningTotal.Caption) - 1 ' ElseIf (KeyAscii = 113 Or KeyAscii = 119 Or KeyAscii = 69 Or KeyAscii = 68 Or KeyAscii = 99 Or KeyAscii = 120 Or KeyAscii = 122) Then ' MsgBox "You shouldn't have done that! Game Over!" ' cmdExit_Click ' ElseIf (KeyAscii = 114) Then ' lblRunningTotal.Caption = Int(Rnd * 100) ' Else ' lblWarning.Visible = True ' End If