' Mr. Minich
' Period 1
' Ch4Demo4
' 11/2/2000
' Purpose - to illustrate the use of the Timer function, the Time function,
'           a TextBox 's KeyPress event, nested If statements, and a method
'           for testing a number's divisibility

Option Explicit

Private Sub Form_Activate()
    ' displays appropriate messages & setting initial visibility
    
    lblMessage.Caption = "Enter a value and press the Enter key:"
    lblOutput.Caption = ""
    
    cmdExit.Caption = "E&xit"       ' setting appropriate messages
    cmdClear.Caption = "&Clear"
    txtInput.Text = ""
    
    lblMessage.AutoSize = True      ' labels will be resized as necessary
    lblOutput.AutoSize = True
    lblCurrentTime.AutoSize = True
    
    lblStartTime.Visible = False    ' hiding the labels used to store times
    lblFinishTime.Visible = False
    
    lblStartTime.Caption = Timer	' storing start time as number of seconds since midnight
    
    lblCurrentTime.Caption = "Program was executed at precisely " & Time
    ' displaying time of program execution
End Sub

Private Sub txtInput_KeyPress(KeyAscii As Integer)
    ' determines if the inputted value is prime or not
    
    Dim intNumberEntered As Integer     ' user's inputted value
    
    If (KeyAscii = 13) Then             ' if the Enter key was pressed
        
        intNumberEntered = Val(txtInput.Text)
        
        If (intNumberEntered = 2 Or intNumberEntered = 3 Or intNumberEntered = 5 _
			Or intNumberEntered = 7 Or intNumberEntered = 11 Or intNumberEntered = 13) Then
			
			lblOutput.Caption = intNumberEntered & " is prime."
ElseIf (intNumberEntered Mod 2 <> 0 And intNumberEntered Mod 3 <> 0 And _ intNumberEntered Mod 5 <> 0 And intNumberEntered Mod 7 <> 0 And _ intNumberEntered Mod 11 <> 0 And intNumberEntered Mod 13 <> 0 And _ intNumberEntered <> 1) Then lblOutput.Caption = intNumberEntered & " is probably prime."
Else lblOutput.Caption = intNumberEntered & " is not prime." End If cmdClear.SetFocus ' setting focus to the Clear button End If End Sub Private Sub cmdClear_Click() ' clears text boxes & sets focus on first text box txtInput.Text = "" ' clearing certain text boxes & labels lblOutput.Caption = "" txtInput.SetFocus ' setting focus on first text box End Sub Private Sub cmdExit_Click() ' unloads form and exits project Dim sngTimeOfExecution As Single ' time of execution lblFinishTime.Caption = Timer ' storing exact time when program ends sngTimeOfExecution = Val(lblFinishTime.Caption) - Val(lblStartTime.Caption) ' computing time of execution MsgBox "Program execution time was " & sngTimeOfExecution & " seconds." Unload Me End End Sub 'Notes ' The Visual Basic Time function is used merely to display the time at the program's ' execution on the form. ' The Visual Basic Timer function is used to store in the Form_Load and cmdExit_Click event procedures ' in order to calculate the amount of seconds that passed during the program's execution. ' Since the Timer function returns the number of seconds since midnight the difference ' between the two stored times is the amount of time that passed during the program's ' execution. Two hidden labels are being used to store these values. You will learn better ' methods for storing these values in variables rather than hidden labels later in the schoolyear. ' A text box's KeyPress event is being used in this program instead of the form's KeyPress event (like you ' saw in Ch4Demo1 & Ch4Demo2. The ASCII value 13 is stored in the KeyAscii parameter when a user presses ' the Enter key. Therefore, only the Enter key will cause the user's value to be tested for divisibility due ' to the outer If statement's control expression. If the user presses the Tab key, the value will not be ' tested. ' The SetFocus method is used in a couple of places so that the program is more user-friendly. This method ' forces the focus to be directed to the applicable object, circumventing all TabIndex properties.

' Why does the program suggest that the value 391 is probably prime when it is not?