Public Class Form1

    '//////// GLOBAL DECLARATIONS
    '//// I/O
    Dim reader1 As IO.StreamReader
    Dim file1 As IO.File
    '//// Computations
    Dim total As Double = 0.0       'Total of all values entered (so far).
    Dim many As Integer = 0         'Count of how many values (so far).

    Private Sub bOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bOpen.Click
        '//// Open the file.
        Dim s As String         ' Filespec string
        '1. Get filespec
        s = Me.iFilespec.Text
        '2. Open the file
        reader1 = IO.File.OpenText(s)
        '3. Say OK
        Me.oResults.Items.Add("File " & s & " is now open.")
    End Sub
    Private Sub bCloseFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bCloseFile.Click
        reader1.Close()     'Close the file.  (NOT the form!)
    End Sub

    Private Sub bNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bNext.Click
        '//// Get next #, add to toal (also keep count)
        Dim s As String         ' Next line from file.
        Dim v As Integer        ' The next value (if any)
        '1. INPUT:  Read the next value (if any).
        If (reader1.Peek() < 0) Then            ' Is there more data?  (See page 262)
            Me.oResults.Items.Add("End of File!")
            Return
        End If
        s = reader1.ReadLine
        v = CInt(s)
        '2. PROCESS:  Update the total and the counter.
        total += v
        many += 1
        '3. OUTPUT: Also display the value (and number).
        Me.oResults.Items.Add(many & ":  " & v)
    End Sub

    Private Sub bTot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bTot.Click
        '//// Display the total
        Me.oResults.Items.Add("The total is " & total)
    End Sub

    Private Sub bAvg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bAvg.Click
        '//// Compute and display the average
        Dim avg As Double
        avg = total / many
        Me.oResults.Items.Add("The average is " & avg)
    End Sub

    Private Sub bClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bClear.Click
        Me.oResults.Items.Clear()       'Clear the output area.
    End Sub
    Private Sub bExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bExit.Click
        Me.Close()          'Close the form.  (NOT the file!)
    End Sub
    Private Sub bRestart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bRestart.Click
        '//// Restart (set total, many back to zero)
        total = 0
        many = 0
    End Sub
End Class
