' Namespace for StreamReader...
Imports System.IO


Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents txtData As System.Windows.Forms.TextBox
    Friend WithEvents btnOpen As System.Windows.Forms.Button
    Friend WithEvents btnClear As System.Windows.Forms.Button
    Friend WithEvents lblCurrentFile As System.Windows.Forms.Label
    Friend WithEvents lblLastFile As System.Windows.Forms.Label
    Friend WithEvents lblLines As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.txtData = New System.Windows.Forms.TextBox()
        Me.btnOpen = New System.Windows.Forms.Button()
        Me.btnClear = New System.Windows.Forms.Button()
        Me.lblCurrentFile = New System.Windows.Forms.Label()
        Me.lblLastFile = New System.Windows.Forms.Label()
        Me.lblLines = New System.Windows.Forms.Label()
        Me.SuspendLayout()
        '
        'txtData
        '
        Me.txtData.Location = New System.Drawing.Point(8, 8)
        Me.txtData.Multiline = True
        Me.txtData.Name = "txtData"
        Me.txtData.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtData.Size = New System.Drawing.Size(304, 192)
        Me.txtData.TabIndex = 0
        Me.txtData.Text = ""
        '
        'btnOpen
        '
        Me.btnOpen.Location = New System.Drawing.Point(320, 8)
        Me.btnOpen.Name = "btnOpen"
        Me.btnOpen.TabIndex = 1
        Me.btnOpen.Text = "Open"
        '
        'btnClear
        '
        Me.btnClear.Location = New System.Drawing.Point(320, 40)
        Me.btnClear.Name = "btnClear"
        Me.btnClear.TabIndex = 2
        Me.btnClear.Text = "Clear"
        '
        'lblCurrentFile
        '
        Me.lblCurrentFile.Location = New System.Drawing.Point(8, 256)
        Me.lblCurrentFile.Name = "lblCurrentFile"
        Me.lblCurrentFile.Size = New System.Drawing.Size(296, 16)
        Me.lblCurrentFile.TabIndex = 3
        Me.lblCurrentFile.Text = "Current file opened: None"
        '
        'lblLastFile
        '
        Me.lblLastFile.Location = New System.Drawing.Point(8, 232)
        Me.lblLastFile.Name = "lblLastFile"
        Me.lblLastFile.Size = New System.Drawing.Size(296, 16)
        Me.lblLastFile.TabIndex = 4
        Me.lblLastFile.Text = "Last file opened: None"
        '
        'lblLines
        '
        Me.lblLines.Location = New System.Drawing.Point(8, 208)
        Me.lblLines.Name = "lblLines"
        Me.lblLines.Size = New System.Drawing.Size(152, 16)
        Me.lblLines.TabIndex = 5
        Me.lblLines.Text = "Number of lines read: 0"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(402, 279)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblLines, Me.lblLastFile, Me.lblCurrentFile, Me.btnClear, Me.btnOpen, Me.txtData})
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "Form1"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Debug Demo"
        Me.ResumeLayout(False)

    End Sub

#End Region
    'Declare  variables
    Dim lastFileName As String = "None"

    Public Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
        ' Declare an object for the OpenFileDialog class...
        Dim myFileDialog As OpenFileDialog = New OpenFileDialog()

        ' Set the dialog properties...
        With myFileDialog
            .Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
            .FilterIndex = 1
            .InitialDirectory = "C:\Temp\"
            .Title = "Open File"
            .CheckFileExists = False
        End With

        ' Show the dialog...
        If myFileDialog.ShowDialog() = DialogResult.OK Then
            ' Process the file chosen...
            ProcessFile(myFileDialog.FileName)
            ' Display the last file opened...
            lblLastFile.Text = "Last file opened: " & lastFileName
            ' Display the current file opened...
            lblCurrentFile.Text = "Current file opened: " & _
                                  myFileDialog.FileName
            ' Save the new file as the last file opened...
            lastFileName = myFileDialog.FileName
        End If

        ' Clean up...
        myFileDialog = Nothing
    End Sub

    Private Sub ProcessFile(ByVal fileName As String)
        ' Declare an object for the StreamReader class...
        Dim myReader As StreamReader

        Try
            ' Open the file...
            myReader = File.OpenText(fileName)
        Catch e As IOException
            ' Display the messages...
            MessageBox.Show(e.ToString)
            MessageBox.Show(e.Message)
            ' Exit the procedure...
            Exit Sub
        End Try


        ' Declare variables...
        Dim lineCounter As Integer = 0
        Dim currentLine As String, currentData As String

        ' Loop through the file counting the lines and loading the 
        ' text box...
        Do
            Try
                ' Read a line from the file...
                currentLine = myReader.ReadLine
                lineCounter = lineCounter + 1
            Catch e As EndOfStreamException
                ' Display the message...
                MessageBox.Show(e.Message)
                ' Exit the Do loop...
                Exit Do
            Finally
                ' Concatenate the data to the strData variable...
                currentData = currentData & currentLine & vbCrLf
            End Try
        Loop While currentLine <> Nothing

        ' Load the text box...
        txtData.Text = currentData

        ' Display the number of lines read...
        lblLines.Text = "Number of lines read: " & _
                              lineCounter - 1

        ' Clean up...
        myReader.Close()
        myReader = Nothing
    End Sub

    Public Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        ' Clear variables...
        lastFileName = ""

        ' Clear form fields...
        txtData.Text = ""

        ' Reset the labels...
        lblLines.Text = "Number of lines read: 0"
        lblLastFile.Text = "Last file opened: None"
        lblCurrentFile.Text = "Current file opened: None"
    End Sub

End Class
