Looping through a file in VB.NET

Posted by Ousman on Stack Overflow See other posts from Stack Overflow or by Ousman
Published on 2010-06-11T08:48:04Z Indexed on 2010/06/11 12:12 UTC
Read the original article Hit count: 285

Filed under:
|
|
|

I am writing a VB.NET program and I'm trying to accomplish the following:

  • Read and loop through a text file line by line
  • Show the event of the loop on a textbox or label until a button is pressed
  • The loop will then stop on any number that happened to be at the loop event and
  • When a button is pressed again the loop will continue.

Code

Imports System.IO    
Public Class Form1


'Dim nFileNum As Integer = FreeFile() ' Get a free file number
Dim strFileName As String = "C:\scb.txt"
Dim objFilename As FileStream = New FileStream(strFileName, _
    FileMode.Open, FileAccess.Read, FileShare.Read)
Dim objFileRead As StreamReader = New StreamReader(objFilename)
'Dim lLineCount As Long
'Dim sNextLine As String


Private Sub btStart_Click(ByVal sender As System.Object, _ 
                          ByVal e As System.EventArgs) _
     Handles btStart.Click

    Try
        If objFileRead.ReadLine = Nothing Then

            MsgBox("No Accounts Available to show!", _
               MsgBoxStyle.Information, _
               MsgBoxStyle.DefaultButton2 = MsgBoxStyle.OkOnly)
            Return
        Else

            Do While (objFileRead.Peek() > -1)

            Loop
            lblAccounts.Text = objFileRead.ReadLine()

            'objFileRead.Close()
            'objFilename.Close()
        End If

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        'objFileRead.Close()
        'objFilename.Close()
    End Try


End Sub

Private Sub Form1_Load(ByVal sender As System.Object, _ 
                       ByVal e As System.EventArgs) _
  Handles MyBase.Load

End Sub
End Class

Problem

I'm able to read the text file but my label will only loop if I hit the start button. It goes to the next line, but I want it to continue to loop through the entire file until I hit a button telling it to stop.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about vb.net