title: Looping through a file in VB.NET
body: <p>I am writing a VB.NET program and I'm trying to accomplish the following:</p>
  
  <ul>
  <li>Read and loop through a text file line by line </li>
  <li>Show the event of the loop on a textbox or label until a button is pressed </li>
  <li>The loop will then stop on any number that happened to be at the loop event and </li>
  <li>When a button is pressed again the loop will continue. </li>
  </ul>
  
  <h3>Code</h3>
  
  <pre><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() &gt; -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
  </code></pre>
  
  <h3>Problem</h3>
  
  <p>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.</p>